【问题标题】:Controller spec: expect(controller).to receive(:create) prevents the #create action from being executed控制器规范:expect(controller).to receive(:create) 阻止执行 #create 操作
【发布时间】:2017-02-23 15:59:06
【问题描述】:

我有以下操作:

def create
  binding.pry
  @finding.save
  respond_with @project, @finding
end

运行以下测试时...

it 'balances the consecutive numbers', focus: true do
  expect {
    post :create, params: {...}
  }.to change { Finding.count }.from(0).to 1
end

...我首先显示binding.pry 控制台(证明#create 操作实际上已执行),然后规范通过:

Finished in 4.24 seconds (files took 5.31 seconds to load)
1 example, 0 failures

现在当我添加expect(controller).to receive(:create)...

it 'balances the consecutive numbers', focus: true do
  expect(controller).to receive(:create) # This is new!

  expect {
    post :create, params: {...}}
  }.to change { Finding.count }.from(0).to 1
end

...再次运行测试,我立即显示了这个规范失败结果:

expected result to have changed from 0 to 1, but did not change

当删除 change { ... } 期望时...

it 'balances the consecutive numbers', focus: true do
  expect(controller).to receive(:create)

  post :create, params: {project_id: @project.id, finding: {requirement_id: @requirement.id}}
end

...它再次通过:

1 example, 0 failures

但是,#create 中的 binding.pry 仍然没有被调用!

那么这里发生了什么?不知何故,expect(controller).to receive(:create) 似乎阻止了实际的 #create 动作被执行!这不是我想要的。我希望它一如既往地执行。

我在这里做错了什么?

【问题讨论】:

  • 我在 AR 回调中遇到了同样的问题。我得到了答案。谢谢。

标签: ruby-on-rails rspec controller mocking


【解决方案1】:

当你使用expect().to receive() 时,真正的方法被抑制了......它只是验证是否按预期调用了某些东西......

当我们想要测试我们无法控制答案或我们想要模拟答案是否被调用时,我们通常会使用它。

如果你想检查是否调用了某些东西,并且还运行原始文件,你应该使用:

expect(something).to receive(:some_method).and_call_original

https://relishapp.com/rspec/rspec-mocks/v/3-5/docs/configuring-responses/calling-the-original-implementation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    相关资源
    最近更新 更多