Reputation: 69
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:
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
Upvotes: 0
Views: 1783
Reputation: 1314
Your suggestions should work actually. But there is another option.
orig_stdin = $stdin
$stdin = StringIO.new('exit')
assert(EchoApp.new.greeting,'Goodbye!')
# Or
expect(EchoApp.new.greeting).to eql('Goodbye!')
$stdin = orig_stdin
Let me know if this does not work.
Upvotes: 2