使用Mocha,我正在存根需要返回2个单独值的相同方法。无论我做什么,它都只返回2个值中的1个,因此我的rspec测试中的一个总是失败。如何让存根在正确的时间返回正确的值?
代码:
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.
如果可用,请尝试使用as_null_object。例如,对于所有具有短截线的线路:
CustomClass.stubs(:app_settings).returns(hash).as_null_object