I have this example:
# GET New
context "on get to new" do
it "should assign cardset" do
@profile.cardsets.expects(:build).once.returns(Factory.stub(:cardset))
get :new
assigns[:cardset].should_not be_nil
end
end
To test this method:
# GET /cardsets/new
def new
@cardset = current_user.cardsets.build
end
I am trying to enforce that the association is built from current_user
to make sure the user is only creating things that belong to themselves. I am using an expectation very similarly to ensure they are calling find
from the current_user
object and it works find, but when running the above example I get:
6)
Mocha::ExpectationError in 'CardsetsController for a logged in user on get to new should assign cardset'
not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: [#<Cardset:0x102eaa8c8>, #<Cardset:0x102e12438>].build(any_parameters)
satisfied expectations:
- allowed any number of times, not yet invoked: ApplicationCont开发者_如何学运维roller.require_user(any_parameters)
- allowed any number of times, already invoked twice: #<CardsetsController:0x1030849c8>.current_user(any_parameters)
/Applications/MAMP/htdocs/my_app/spec/controllers/cardsets_controller_spec.rb:32:
You add the expectation to @profile after you've stubbed the function that returns it from current_user. Probably what you need to do is this:
# GET New
context "on get to new" do
it "should assign cardset" do
@profile.cardsets.expects(:build).once.returns(Factory.stub(:cardset))
controller.stubs(:current_user).returns(@profile)
get :new
assigns[:cardset].should_not be_nil
end
end
精彩评论