Q:

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

How would I stub a method that takes two user inputs using rspec? Is it 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 do

    it "should echo" do
        @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 the return from @mirror.echo is ["bar", "bar"] meaning that the first stub is overwritten or otherwise ignored.
I have also tried using @mirror.stub!(:gets){"foo\nbar\n"} and @mirror.echo returns ["foo\nbar\n","foo\nbar\n"]

A:

You can use and_return method to return different values each time method is called.
@mirror.stub!(:gets).and_return("foo\n", "bar\n")

And your code would look like this
it "should echo" do
  @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

