Ruby 如何监视ActiveSupport::Notifications工具的一个调用,而不是所有调用

Ruby 如何监视ActiveSupport::Notifications工具的一个调用,而不是所有调用,ruby,rspec,factory-bot,activesupport,rspec-mocks,Ruby,Rspec,Factory Bot,Activesupport,Rspec Mocks,我正在做一个Rspec测试,检查是否使用一些参数调用了ActiveSupport::Notification.instrument 问题是,为了进行此测试,需要FactoryBot构建一些对象,但当我尝试监视ActiveSupport::Notification.instrument时,我总是会收到一个错误: ActiveSupport::Notifications received :instrument with unexpected arguments expected:

我正在做一个Rspec测试,检查是否使用一些参数调用了
ActiveSupport::Notification.instrument

问题是,为了进行此测试,需要
FactoryBot
构建一些对象,但当我尝试监视
ActiveSupport::Notification.instrument
时,我总是会收到一个错误:

ActiveSupport::Notifications received :instrument with unexpected arguments
         expected: (:asd)
              got: ("factory_bot.run_factory", {:factory=>#<FactoryBot::Factory:0x005569b6d30, @al... nil, dispatch: nil, distribution_state: 2, main_category_id: nil>}, :strategy=>:build, :traits=>[]})
规格:

我怎么能嘲笑我的电话而不是FactoryBot的

我只需要在模拟的
:asd
之前再添加一个
allow

 allow(ActiveSupport::Notifications).to receive(:instrument).and_call_original

还有别的(更好的)方法吗?

我一般都避免嘲笑

我有一个类似的问题,下面是我如何做到的:

  describe "#my_method" do
    let(:some_object) { build :some_object }

    before { record_events }

    it "calls notifier" do
      described_class.new(some_object).my_method

      # Make sure your event was triggered
      expect(events.map(&:name)).to include('asd')

      # Check number of events
      expect(events).to be_one

      # Check contents of event payload                  
      expect(events.first.payload).to eq({ 'extra' => 'context' })

      # Even check the duration of an event
      expect(events.first.duration).to be < 3
    end

    private

    attr_reader :events

    def record_events
      @events = []
      ActiveSupport::Notifications.subscribe(:asd) do |*args| #
        @events << ActiveSupport::Notifications::Event.new(*args)
      end
    end
  end
描述“我的方法”是什么
let(:some_object){build:some_object}
在{记录事件}之前
它“调用通知程序”做什么
描述了\u类。新(一些\u对象)。我的\u方法
#确保您的事件已触发
期望(events.map(&:name))。包括('asd')
#检查事件数
期待(事件)。成为一个
#检查事件负载的内容
expect(events.first.payload).to eq({'extra'=>'context'})
#甚至可以检查事件的持续时间
期望(事件、第一个事件、持续时间)小于3
结束
私有的
属性读取器:事件
def记录事件
@事件=[]
ActiveSupport::Notifications.subscribe(:asd)do |*args |#
@事件
 allow(ActiveSupport::Notifications).to receive(:instrument).and_call_original
  describe "#my_method" do
    let(:some_object) { build :some_object }

    before { record_events }

    it "calls notifier" do
      described_class.new(some_object).my_method

      # Make sure your event was triggered
      expect(events.map(&:name)).to include('asd')

      # Check number of events
      expect(events).to be_one

      # Check contents of event payload                  
      expect(events.first.payload).to eq({ 'extra' => 'context' })

      # Even check the duration of an event
      expect(events.first.duration).to be < 3
    end

    private

    attr_reader :events

    def record_events
      @events = []
      ActiveSupport::Notifications.subscribe(:asd) do |*args| #
        @events << ActiveSupport::Notifications::Event.new(*args)
      end
    end
  end