控制器规范:expect(控制器).to receive(:create)阻止#create操作被执行

时间:2017-02-23 15:59:07

标签: ruby-on-rails rspec controller mocking

我有以下行动:

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

但是binding.pry中的#create仍未被调用!

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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

当你使用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