2012-07-17 128 views
1

我遇到了問題,在我的Rails應用程序中使用RSpec和decent_exposure gem。Rspec測試失敗與decent_exposure寶石

由於decent_exposure調用方法「new」兩次(Model.new(params [name]),我的控制器測試失敗,一次使用名稱(Brand.new(params [「品牌」))返回Brand.new (Brand.new(params [「brand」]))。我需要以某種方式跳過我的測試文件中的第一個電話。Brand.should_receive(:new).with(...)。once。 and_return(帶)不工作

我的測試文件:?

let(:brand) { 
    mock_model(Brand).as_null_object 
} 

before do 
    Brand.stub(:new).and_return(brand) 
end 

describe "with valid parameters" do 
    it "should create a new brand" do 
    Brand.should_receive(:new).with(
     "name" => "LG", 
    ).and_return(brand) 

    post :create, :brand => { 
     "name" => "LG", 
    } 
    end 
end 

所以,可以請你幫我找出如何讓通過這個

回答

0

試試這個:

Brand.should_receive(:new).once.with(any_args()) 
Brand.should_receive(:new).once.with("name" => "LG").and_return(brand) 

我建議增加對任何方法的控制器使用堅持brand的期望。通常這是save

brand.should_receive(:save) { true }