ruby - rspec - How can I stub a method with multiple user inputs? -


how stub method takes 2 user inputs using rspec? possible?

class mirror     def echo         arr = []         print "enter something: "         arr[0] = gets.chomp         print "enter something: "         arr[1] = gets.chomp          return arr     end end    describe mirror      "should echo"         @mirror = mirror.new         @mirror.stub!(:gets){   "foo\n" }         @mirror.stub!(:gets){   "bar\n" }         arr = @mirror.echo         #@mirror.should_receive(:puts).with("phrase")         arr.should eql ["foo", "bar"]      end  end 

with these specs return @mirror.echo ["bar", "bar"] meaning first stub overwritten or otherwise ignored.

i have tried using @mirror.stub!(:gets){"foo\nbar\n"} , @mirror.echo returns ["foo\nbar\n","foo\nbar\n"]

you can use and_return method return different values each time method called.

@mirror.stub!(:gets).and_return("foo\n", "bar\n") 

and code this

it "should echo"   @mirror = mirror.new   @mirror.stub!(:gets).and_return("foo\n", "bar\n")   @mirror.echo.should eql ["foo", "bar"] end 

example of using and_return

counter.stub(:count).and_return(1,2,3) counter.count # => 1 counter.count # => 2 counter.count # => 3 counter.count # => 3 counter.count # => 3 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -