如何使用rspec测试条件ActiveRecord after_update回调? [英] How to test conditional ActiveRecord after_update callback with rspec?

查看:61
本文介绍了如何使用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

这篇关于如何使用rspec测试条件ActiveRecord after_update回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆