2013-02-13 76 views
0

使用摩卡,我存根需要返回2個單獨的值相同的方法。無論我做什麼,它只返回2個值中的1個,因此我的rspec測試中有1個總是失敗。如何讓存根在正確的時間返回正確的值?同樣的方法摩卡需要返回2個不同的值

代碼:

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

您是否在嘗試調用'CustomClass.app_settings'後立即驗證它是否返回了預期的散列值?這可能有助於縮小問題範圍,消除其他問題導致您的響應出現意外計數的任何疑問。 – exbinary 2013-02-13 02:41:06

+0

an4rcho,你的建議做到了。殘樁工作正常。問題出在'get'method''本身。它沒有正確檢查值。 – 2013-02-16 21:06:49

回答

1

如果可用,請嘗試使用as_null_object。所以例如對於所有具有存根的線路:

CustomClass.stubs(:app_settings).returns(hash).as_null_object 
相關問題