shoulda-callback-matchers after_save 条件问题

shoulda-callback-matchers after_save problem with condition

我正在使用 shoulda-callback-matchers gem 来测试我的回调。 但是我的 after_savecondition 有问题。

回调在model:

after_save :update_effort_rate, if: -> { saved_change_to_rent? }

我的测试 spec:

context 'callbacks' do
  it { is_expected.to callback(:update_effort_rate).after(:save).if :rent_changed? }
end

Rspec 错误:

Failure/Error: it { is_expected.to callback(:update_effort_rate).after(:save).if :saved_change_to_rent? }
       expected update_effort_rate to be listed as a callback after save if saved_change_to_rent? evaluates to true, but was not

我不知道我做错了什么。有什么帮助吗?谢谢

尝试通过仅传入方法名称(而不是调用方法的 lambda)来更改 if 子句:

after_save :update_effort_rate, if: :saved_change_to_rent?

https://guides.rubyonrails.org/active_record_callbacks.html#using-if-and-unless-with-a-symbol