我有一个触发作业的条件回调。如果经理发生变化,则应调用方法
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
我无法对此进行测试。我需要像
这样的东西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
任何人都知道最好的方法是什么?
答案 0 :(得分:3)
我最终使用了gem shoulda-callback-matchers
context 'callbacks' do
it { is_expected.to callback(:employee_manager_on_change).after(:update).if :manager_id_changed? }
end
答案 1 :(得分:0)
有这样的吗?
context 'when changing manager' do
before { user.manager = new_manager }
it 'calls employee_manager_on_change' do
user.save
expect(user).to receive(:employee_manager_on_change)
end
end
您甚至可以指定方法必须被调用的时间
expect(user).to receive(:employee_manager_on_change).once