期望发现不起作用,但对find_by_id的期望是
-
02-10-2019 - |
题
我有此控制器代码:
# GET /cardsets/1
def show
@cardset = current_user.cardsets.find_by_id(params[:id])
end
和此RSPEC测试代码(用摩卡摩卡嘲笑):
# GET Show
context "on get to show" do
it "should assign cardset" do
@cardset = Factory(:cardset)
@profile = @cardset.profile
@profile.cardsets.expects(:find).once.returns(@cardset)
get :show, :id => @cardset.id
assigns[:cardset].should_not be_nil
end
end
该测试失败了:
2)
Mocha::ExpectationError in 'CardsetsController for a logged in user on get to show should assign cardset'
not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: [#<Cardset:0x1032bb660>].find(any_parameters)
satisfied expectations:
- allowed any number of times, not yet invoked: ApplicationController.require_user(any_parameters)
- allowed any number of times, already invoked twice: #<CardsetsController:0x10336c578>.current_user(any_parameters)
如果我改变了期望:
@profile.cardsets.expects(:find_by_id).once.returns(@cardset)
然后测试通过了,为什么会通过find_by_id且找不到找到?
解决方案
我认为这是因为 find
和 find_by_id
实际上是不同的消息。您的控制器使用 find_by_id
, ,但是您正在设置消息期望 find
.
不隶属于 StackOverflow