[英]Rspec controller test fails
i'm trying to run such test: 我正在尝试运行这样的测试:
it "render form to update an bundle with a specific id" do
bundle = mock_model(Bundle)
Bundle.stub!(:find).with("1") { bundle }
get :edit, :locale => "en", :id => 1
Bundle.should_receive(:find).with("1").and_return(bundle)
end
Code from a Controller: 来自控制器的代码:
class BundlesController < ApplicationController
# GET /bundles
# GET /bundles.json
.....
# GET /bundles/1/edit
def edit
@bundle = Bundle.find(params[:id])
end
.....
end
But test fails with message: 但测试失败并显示消息:
BundlesController Bundle update render form to update an bundle with a specific id Failure/Error: Bundle.should_receive(:find).with("1").and_return(bundle) ().find("1") expected: 1 time received: 0 times # ./spec/controllers/bundles_controller_spec.rb:60:in `block (3 levels) in ' BundlesController Bundle更新渲染表单以更新具有特定id的bundle Failure / Error:Bundle.should_receive(:find).with(“1”)。and_return(bundle)()。find(“1”)expect:1次收到:0次#./spec/controllers/bundles_controller_spec.rb:60:in`block(3级)in'
Can anyone helps me? 谁能帮助我? Thanks!
谢谢!
There are a couple of problems here, and maybe more as you post more of your code. 这里有一些问题,也许更多的是你发布了更多的代码。
First of all, you're setting up stubs and expectations on Bundle
and then showing us code that loads a Role
instead. 首先,您要在
Bundle
上设置存根和期望,然后向我们展示加载Role
代码。
Second, you're calling #should_receive
at the end of your test. 其次,你在测试结束时调用
#should_receive
。 This method sets up an expectation for code that comes after it in your test. 此方法设置了对测试之后的代码的期望。 Unless you have some hidden callback that you're not showing us, this is always going to fail.
除非你有一些你没有向我们展示的隐藏回调,否则这总会失败。 Reverse the order.
颠倒顺序。
Bundle.should_receive(:find).with("1").and_return(bundle)
get :edit, :locale => "en", :id => 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.