我有以下操作:
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...I首先显示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...and再次运行测试,我立即显示此规范失败的结果:
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...it再次通过:
1 example, 0 failures但是,#create中的binding.pry仍然没有被调用!
那么这是怎么回事呢?不知何故,expect(controller).to receive(:create)似乎阻止了实际的#create操作的执行!这不是我想要的。我希望它像往常一样执行。
我在这里做错了什么?
发布于 2017-02-25 00:46:59
当你使用expect().to receive()时,真正的方法是被抑制的。它只是验证是否如预期那样调用了某些东西...
我们通常在我们想要测试的时候使用它,如果我们不能控制答案,或者我们想模拟答案是被调用的。
如果您希望检查是否调用了某些内容,并同时运行原始程序,则应使用:
expect(something).to receive(:some_method).and_call_originalhttps://stackoverflow.com/questions/42420582
复制相似问题