Mocha 对关联构建调用的期望失败

发布于 2024-09-08 19:21:43 字数 1171 浏览 5 评论 0原文

我有这个例子:

# 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 构建关联,以确保用户只创建属于自己的东西。我使用非常类似的期望来确保它们从 current_user 对象调用 find 并且它可以工作 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:

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: 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:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱本泡沫多脆弱 2024-09-15 19:21:43

在对从 current_user 返回期望的函数进行存根后,将期望添加到 @profile 中。也许你需要做的是这样的:

# 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文