【发布时间】:2019-02-05 16:29:49
【问题描述】:
我试图确保在被回调调用时调用给定的方法。
基本上,我有一个 RiskMatrix 模型,它在回调after_save 上调用 2 个私有方法
所以我正在尝试测试这些方法是否被正确调用。
非常感谢您的帮助
class RiskMatrix < ActiveRecord::Base
after_save :alert_admin, :save_suspicious_ip, if: proc {score.length >= ALERT_THRESHOLD}
private
def alert_admin
[...]
end
def save_suspicious_ip
[...]
end
end
risk_matrix_spec.rb
describe 'after_save' do
context 'score.length > ALERT_THRESHOLD' do
it 'should run alert_admin' do
matrix = build(:risk_matrix, score: 'ABCD')
expect(matrix).to receive(:alert_admin)
end
it 'should run save_suspicious_ip' do
matrix = create(:risk_matrix, score: 'ABCD')
expect(matrix).to receive(:save_suspicious_ip)
end
end
end
两个测试都失败了
(#<RiskMatrix id: 3201, score: "ABCD", user_id: 3115, created_at: "2019-02-05 16:27:01", updated_at: "2019-02-05 16:27:01">).alert_admin(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments
(#<RiskMatrix id: nil, score: "ABCD", user_id: nil, created_at: nil, updated_at: nil>).save_suspicious_ip(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments
【问题讨论】:
-
从我的角度来看,这应该回答你的问题stackoverflow.com/a/42293469/7654691
标签: ruby-on-rails-4 rspec