gpt4 book ai didi

ruby-on-rails - 使用 RSpec 和模拟测试 after_commit

转载 作者:数据小太阳 更新时间:2023-10-29 06:26:12 25 4
gpt4 key购买 nike

我有一个模型 Lead 和一个回调: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 回调的最佳方法是什么?

最佳答案

我以为Mihail Davydenkov's comment当之无愧的答案:

You can also use subject.run_callbacks(:commit).

另请注意,此问题(在事务测试中未调用提交回调)应在 rails 5.0+ 中得到修复,因此您可能希望在升级期间删除可能使用的任何解决方法。请参阅:https://github.com/rails/rails/pull/18458

关于ruby-on-rails - 使用 RSpec 和模拟测试 after_commit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22988968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com