0

How can I unit test that my JSON API resource callback is been registered as after_create, and that send_notifications is invoked, and I can expect(MyMailer).to receive(:welcome_email)?

When I attempt to allow & expect MyResource#send_notifications, or MyMailer.welcome_email they calls are not tracked.

    class MyResource < BaseResource

      after_create :send_notifications

      def send_notifications
        MyMailer.welcome_email(_model).deliver
      end
    end


    # rspec

    RSpec.describe Creem::Jpi::V1::MyResourceResource, type: :resource do
      include JsonApiResourceHelpers

      let(:my_resource) { create(:my_resource) }
      subject { described_class.new(my_resource, {}) }

      it { expect(described_class).to be < BaseResource }

      describe 'callbacks' do
        xit 'how do I unit test my callbacks?'
      end
    end
xaunlopez
  • 439
  • 1
  • 4
  • 13

1 Answers1

1

I solved this by functionally testing the mailer.

This can be done by adding a test case to the controller spec for the controller action which would trigger the resource callback.

    RSpec.describe MyResourceController, type: :controller do
      describe 'POST #create' do
        subject { post :create, params: { data: data } }

        it 'sends new notifications' do
          expect { subject }.to change { ActionMailer::Base.deliveries.size }.by(1)
        end
      end
    end
xaunlopez
  • 439
  • 1
  • 4
  • 13