【发布时间】:2013-03-14 08:26:37
【问题描述】:
如何使用 rspec 存根需要两个用户输入的方法?有可能吗?
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
使用这些规范,@mirror.echo 的返回值是 ["bar", "bar"],这意味着第一个存根被覆盖或以其他方式忽略。
我也尝试过使用@mirror.stub!(:gets){"foo\nbar\n"} 并且@mirror.echo 返回 ["foo\nbar\n","foo\nbar\n"]
【问题讨论】: