我在Rails应用程序中使用RSpec和decent_exposure gem时遇到了问题。
我的控制器测试失败了,因为decent_exposure调用了两次方法"new“(Model.new(paramsname)。一次使用name (Brand.new(params“Brand.new”)返回Brand.new(nil)),第二次使用我期望的(Brand.new(params"brand"))。我需要在我的测试文件中跳过第一个调用。Brand.should_receive(:new).with(...).once.and_return(band)不工作。
我的测试文件:
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所以,你能帮我弄清楚怎么才能通过吗?
发布于 2012-07-18 05:12:15
试试这个:
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 }https://stackoverflow.com/questions/11522567
复制相似问题