I have two questions. Given the ruby code below, what kind of tests would your write in RSpec and how would you avoid when running tests to keep get asked for the user input? I have tried many ways such as:
- allow($stdout)
- allow(Kernel)
- stub(:gets)
- STDOUT.should_receive(:puts).with("exit")
- allow($stdin).to receive(:gets).and_return('no_gets')
- allow_any_instance_of(Kernel).to receive(:gets).and_return('exit')
But non of the above worked. In test kept asking me for the user input.
class EchoApp
def greeting
while(true)
puts "Say something:"
user = gets.chomp
break if user == 'exit'
end
return "Goodbye!"
end
end