Problem
Hello! For the next Ruby method, how can I simulate user input using the RSpec test without overwriting the method?
def capture_name puts "What is your name?" gets.chomp end
I found a similar question , but this approach requires creating using a class. Does RSpec support stubbing for methods outside the class?
Various works, but I have to rewrite the method
I can rewrite the method so that it has a variable with the default value "gets.chomp" as follows:
def capture_name(user_input = gets.chomp) puts "What is your name?" user_input end
Now I can write the RSpec test as follows:
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
source share