2010-07-07 56 views
0

我有这样的控制器代码:展望为找不工作,而是期望的find_by_id是

# 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而找不到?

回答

1

我认为这是因为findfind_by_id实际上是不同的消息。您的控制器使用find_by_id,但您将消息期望设置为寻找find

+0

不能相信我没有看到。我有另一个类似的问题,但。 #new操作的设置与#show操作相同,只是在使用此@ profile.cardsets.expects(:build).returns(Factory.stub(:cardset))'时调用'current_user.cardsets.build'到期待它,我得到一个失败的测试,说它没有被调用。 – trobrock 2010-07-07 22:40:53

相关问题