加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – `update`上的`after_commit`回调不会触发

发布时间:2020-12-17 03:18:27 所属栏目:百科 来源:网络整理
导读:我在更新时定义了after_commit回调.它不会在rspec中触发. 这是我的回调: after_commit :notify_trip,:if = Proc.new { |trip| trip.can_send_schedule_notification? },on: :update 这是我的rspec代码: @work_order.update_attributes(:status_id = status
我在更新时定义了after_commit回调.它不会在rspec中触发.

这是我的回调:

after_commit :notify_trip,:if => Proc.new { |trip| trip.can_send_schedule_notification? },on: :update

这是我的rspec代码:

@work_order.update_attributes(:status_id => statuses(:scheduled).id,:tour_id => @tour.id)
@work_order.run_callbacks(:commit)
assert_equal 1,ActionMailer::Base.deliveries.size
expect(ActionMailer::Base.deliveries[0].to).to include(@user.email)
expect(ActionMailer::Base.deliveries[0].subject).to include("Your trip has been scheduled!")

这里回调没有调用,ActionMailer :: Base.deliveries.size返回0

对此有什么建议吗?

解决方法

根据 documentation,你需要使用 test_after_commit gem来让after_commit钩子在测试中被触发.在rails 5.0上将不再需要这个.

另一个替代方法是将以下代码放在it块的主体末尾:

subject.run_callbacks(:commit)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读