I'm trying to make sure that a given method is being called when called by a callback.
Basically, I have a RiskMatrix model, that calls 2 private methods on callback after_save
So I'm trying to test that those methods are properly called.
thanks a lot for your help
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
both tests fail
(#<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