method
assert_notification
v8.1.1 -
Show latest stable
- Class:
ActiveSupport::Testing::NotificationAssertions
assert_notification(pattern, payload = nil, &block)public
Assert a notification was emitted with a given pattern and optional payload.
You can assert that a notification was emitted by passing a pattern, which accepts either a string or regexp, an optional payload, and a block. While the block is executed, if a matching notification is emitted, the assertion will pass and the notification will be returned.
Note that the payload is matched as a subset, meaning that the notification must contain at least the specified keys and values, but may contain additional ones.
assert_notification("post.submitted", title: "Cool Post") do post.submit(title: "Cool Post", body: "Cool Body") # => emits matching notification end
Using the returned notification, you can make more customized assertions.
notification = assert_notification("post.submitted", title: "Cool Post") do ActiveSupport::Notifications.instrument("post.submitted", title: "Cool Post", body: Body.new("Cool Body")) end assert_instance_of(Body, notification.payload[:body])