1

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
Mike W
  • 391
  • 4
  • 14

2 Answers2

3

You can use shoulda-callback-matchers to test your callbacks

it { is_expected.to callback(:alert_admin).after(:save) }

The other way to test is verify that after save a matrix a suspicious_ip must exist.

let(:matrix) { create(:risk_matrix, score: 'ABCD') }
context "when score.length > ALERT_THRESHOLD" do
  it "after create a matrix" do
   expect(matrix.suspicious_ip).to_not be_nil
  end
end
context "when score.length < ALERT_THRESHOLD" do
  it "after create a matrix" do
   expect(matrix.suspicious_ip).to be_nil
  end
end
Luis Silva
  • 201
  • 2
  • 5
1

Maybe I am missing something but I am not seeing the save calls, have you tried:

it 'should run alert_admin' do
  matrix = build(:risk_matrix, score: 'ABCD')
  allow(matrix).to receive(:alert_admin)
  matrix.save!
  expect(matrix).to have_received(:alert_admin).once
end

Before you save you let RSpec know you gonna stub that method, after the save you verify the method was called.

Danilo Cabello
  • 2,814
  • 1
  • 23
  • 26