Waiting for mocha when assembly call fails

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 ensure that the association is built out current_userto ensure that the user creates only what belongs to them. I use expectation very much like they are calling findfrom an object current_userand it works, but when I run 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: ApplicationController.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:
+3
source share
1 answer

You add a wait to @profile after you run a function that returns it from current_user. You probably need to do the following:

# 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
+1
source

Source: https://habr.com/ru/post/1754274/


All Articles