Мокко ожидание сбоя вызова сборки ассоциации - PullRequest
3 голосов
/ 12 июля 2010

У меня есть этот пример:

# 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

Чтобы проверить этот метод:

# GET /cardsets/new
def new
  @cardset = current_user.cardsets.build
end

Я пытаюсь убедиться, что связь построена из current_user, чтобы убедиться, что пользовательсоздает только вещи, которые принадлежат им самим.Я использую ожидание очень похожим образом, чтобы убедиться, что они вызывают find из current_user объекта, и он работает find, но при запуске приведенного выше примера я получаю:

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:

1 Ответ

1 голос
/ 03 ноября 2011

Вы добавляете ожидание к @profile после того, как заглушите функцию, которая возвращает его из current_user.Вероятно, вам нужно сделать следующее:

# 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
...