im trying to test a user input-influenced until loop, but am stumped on how to pull it off... i need some inspiration. how would you go about this?
class Menu
@@menu = nil
def get_menu
menu = nil
until menu == 'foo'
menu = Readline.readline
end
@@menu = menu
end
end
describe '.get_menu' do
before do
Readline.stub(:readline).and_return('bar')
Menu.new.get_menu
end
it 'should not set class var' do
expect(Menu.menu).to be_nil
end
end
this is completely contrived and probably doesn't actually run, but you get the idea... (i'm using readline for tab completion, but its the same issue with gets/stdin)
a few details on gotva's solution
Since i needed to eventually set 'foo' to prevent an infinite loop, i had to test the mocks instead of the end value of Menu.menu
Readline.stub(:readline).and_return('bar', 'par', 'foo')
expect(Readline).to have_received(:readline).exactly(3).times