【发布时间】: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