使用 rspec 测试 ActiveSupport::Notifications
有人知道如何指定主动支持通知吗?以下似乎不起作用。它检测默认的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,并在下面编写了以下 rspec 辅助方法:
这样,我可以按如下方式使用它:
I had the same issue and wrote the following rspec helper method below:
This way, I can use it as follows:
我无法使用常规
:get
操作来实现测试,应该会受到某种干扰,并且只会触发start_processing
和process_action
通知。我猜它是出于性能考虑而被禁用的。但这成功地起作用了:
I was unable to achieve the test using a regular
:get
action, something should interfere somehow and onlystart_processing
andprocess_action
notifications are triggered. I guess it has been disabled for performance sake.But this successfully works: