【发布时间】:2014-05-24 05:09:05
【问题描述】:
我有一个模型主管和一个回调:after_commit :create, :send_to_SPL
我正在使用 Rails-4.1.0、ruby-2.1.1、RSpec。
1) 此规范未通过:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(lead).to receive(:send_to_SPL)
lead = Lead.create(init_hash)
p lead.new_record? # => false
end
end
2) 这个规范也没有通过:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(ActiveSupport::Callbacks::Callback).to receive(:build)
lead = Lead.create(init_hash)
end
end
3)这个是通过的,但我认为它不是在测试 after_commit 回调:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(lead).to receive(:send_to_SPL)
lead.send(:send_to_SPL)
end
end
在 Rails 中测试 after_commit 回调的最佳方法是什么?
【问题讨论】:
标签: ruby-on-rails ruby rspec mocking