Ruby on rails 测试Rails JSON API资源回调

Ruby on rails 测试Rails JSON API资源回调,ruby-on-rails,jsonapi-resources,Ruby On Rails,Jsonapi Resources,我怎样才能单元测试我的JSON API资源回调在创建后注册为,发送通知被调用,并且我可以期望(MyMailer)。接收(:welcome\u email) 当我尝试允许和期望MyResource\35;发送通知或MyMailer.welcome\u电子邮件时,不会跟踪他们的呼叫 class MyResource < BaseResource after_create :send_notifications def send_notifications

我怎样才能单元测试我的JSON API资源回调在创建后注册为
,发送通知被调用,并且我可以
期望(MyMailer)。接收(:welcome\u email)

当我尝试
允许
期望
MyResource\35;发送通知或MyMailer.welcome\u电子邮件时,不会跟踪他们的呼叫

    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
class MyResource
我通过对邮件程序进行功能测试解决了这个问题

这可以通过在控制器规范中为控制器操作添加一个测试用例来完成,该操作将触发资源回调

    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