Challenge
Hi! For the following Ruby method, how might I simulate user input using an RSpec test without rewriting the method?
def capture_name
puts "What is your name?"
gets.chomp
end
I've found a similar question, but this approach requires creating using a class. Does RSpec support stubbing for methods not in a class?
A different works, but I'm forced to rewrite the method
I can rewrite the method so it has a variable with the default value of "gets.chomp" like this:
def capture_name(user_input = gets.chomp)
puts "What is your name?"
user_input
end
Now I can write an RSpec test like this:
describe "Capture name" do
let(:user_input) { "James T. Kirk" }
it "should be 'James T. Kirk'" do
capture_name(user_input).should == "James T. Kirk"
end
end