使用 rspec 测试 ActiveSupport::Notifications

发布于 2024-12-01 05:41:14 字数 556 浏览 1 评论 0原文

有人知道如何指定主动支持通知吗?以下似乎不起作用。它检测默认的 Rails 框架通知,但不检测我的自定义通知。

it 'sends a "product.search" notification to any subscribers listening'
  ActiveSupport::Notifications.should_receive(:instrument).with("product.search", :search => search) 
  get :search, ...
end

如果我更改规范来检查订阅者代码的结果(例如,创建数据库记录时记录计数发生变化),它就会通过。这证实它工作正常。但是,指定订阅者在这里执行的操作似乎是错误的,我只想指定正在发送通知。任何想法将不胜感激。

编辑:

这是我尝试指定的控制器代码:

ActiveSupport::Notifications.instrument("product.search", :search => 'test')

Does anybody know how you can spec an active support notification? The following doesn't seem to work. It detects the default rails framework notifications but not my custom one.

it 'sends a "product.search" notification to any subscribers listening'
  ActiveSupport::Notifications.should_receive(:instrument).with("product.search", :search => search) 
  get :search, ...
end

If I change the spec to check the outcome of the subscriber's code (e.g. record count change when creating a DB record) it passes. That confirms that it is working ok. But, it seems wrong to spec what the subscriber does here, I just want to spec that the notification is being sent. Any thoughts would be appreciated.

EDIT:

Here is the controller code that I'm trying to spec:

ActiveSupport::Notifications.instrument("product.search", :search => 'test')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法回应 2024-12-08 05:41:14

我遇到了同样的问题,并在下面编写了以下 rspec 辅助方法:

  def notification_payload_for(notification)
    payload = nil
    subscription = ActiveSupport::Notifications.subscribe notification do |name, start, finish, id, _payload|
      payload = _payload
    end

    yield

    ActiveSupport::Notifications.unsubscribe(subscription)

    return payload
  end

这样,我可以按如下方式使用它:

 it "should raise the my_notification_name notification" do
    payload = notification_payload_for('my_notification_name') do
      # do stuff that should raise the proper notification
    end

    # test to see that the payload has the correct info
  end

I had the same issue and wrote the following rspec helper method below:

  def notification_payload_for(notification)
    payload = nil
    subscription = ActiveSupport::Notifications.subscribe notification do |name, start, finish, id, _payload|
      payload = _payload
    end

    yield

    ActiveSupport::Notifications.unsubscribe(subscription)

    return payload
  end

This way, I can use it as follows:

 it "should raise the my_notification_name notification" do
    payload = notification_payload_for('my_notification_name') do
      # do stuff that should raise the proper notification
    end

    # test to see that the payload has the correct info
  end
凝望流年 2024-12-08 05:41:14

我无法使用常规 :get 操作来实现测试,应该会受到某种干扰,并且只会触发 start_processingprocess_action 通知。我猜它是出于性能考虑而被禁用的。

但这成功地起作用了:

it 'sends a "product.search" notification to any subscribers listening'
  ActiveSupport::Notifications.should_receive(:instrument).with("product.search", :search => "test")
  controller.index
end

I was unable to achieve the test using a regular :get action, something should interfere somehow and only start_processing and process_action notifications are triggered. I guess it has been disabled for performance sake.

But this successfully works:

it 'sends a "product.search" notification to any subscribers listening'
  ActiveSupport::Notifications.should_receive(:instrument).with("product.search", :search => "test")
  controller.index
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文