ActiveSupport Testing :: Notifications with rspec

Does anyone know how you can indicate an active support notification? The following does not work. It detects notifications about the basics of rails by default, 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 specification to check the result of the subscriber’s code (for example, changing the number of records when creating a database record), it passes. This confirms that it is working properly. But it seems wrong to specify what the subscriber is doing here, I just want to indicate that a notification is being sent. Any thoughts would be appreciated.

EDIT:

Here is the controller code I'm trying to specify:

 ActiveSupport::Notifications.instrument("product.search", :search => 'test') 
+6
source share
2 answers

I had the same problem and wrote the following rspec helper method:

  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 

Thus, 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 
+5
source

I was not able to run the test using the regular action :get , something must somehow interfere, and start_processing and process_action notifications start_processing . I assume it has been disabled to improve performance.

But this works successfully:

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

Source: https://habr.com/ru/post/895771/


All Articles