使用Mocha,我正在使用需要返回2个单独值的相同方法。无论我做什么,它只返回2个值中的1个,因此我的一个rspec测试总是失败。如何让stub在正确的时间返回正确的值?
代码:
describe "#method" do
it "has something" do
hash = { "allow_sharing" => "1"}
CustomClass.stubs(:app_settings).returns(hash)
get 'method', :format => :json
JSON.parse(response.body).count.should eq(1)
end
it "does not have something" do
hash = { "allow_sharing" => "0"}
CustomClass.stubs(:app_settings).returns(hash)
get 'method', :format => :json
JSON.parse(response.body).count.should eq(0)
end
end
我也用before
块尝试了这种方式。仍然没有运气。
describe "#method" do
before do
hash = { "allow_sharing" => "1"}
CustomClass.stubs(:app_settings).returns(hash)
end
it "has something" do
get 'method', :format => :json
JSON.parse(response.body).count.should eq(1)
end
# ... etc.
答案 0 :(得分:1)
如果可用,请尝试使用as_null_object。例如,对于所有带存根的行:
CustomClass.stubs(:app_settings).returns(hash).as_null_object