如何使用rspec测试条件ActiveRecord after_update回调?
问题描述:
我有一个触发工作的条件回调.如果经理更改,则应调用方法
I have a conditional callback that triggers a job. If the manager changes, it should call the method
class Employee < ActiveRecord::Base
after_update :employee_manager_on_change, if: :employee_id_changed?
def employee_manager_on_change
EmployeeManagerChangedJob.perform_later(id)
end
end
我无法对此进行测试.我需要类似的东西
I'm having trouble to test this. I needed something like
context 'when changing manager' do
subject { user.manager = new_manager }
it 'calls employee_manager_on_change' do
expect { suject.run_callbacks :update }.to receive(:employee_manager_on_change)
end
end
任何人都知道什么是最好的方法吗?
Anyone knows what's the best approach for this?
答
我最终使用了宝石 shoulda-callback-matchers
context 'callbacks' do
it { is_expected.to callback(:employee_manager_on_change).after(:update).if :manager_id_changed? }
end