method

assert_event_reported

rails latest stable - Class: ActiveSupport::Testing::EventReporterAssertions

Method not available on this version

This method is only available on newer versions. The first available version (v8.1.1) is shown here.

assert_event_reported(name, payload: nil, tags: {}, &block)
public

Asserts that the block causes an event with the given name to be reported to Rails.event.

Passes if the evaluated code in the yielded block reports a matching event.

assert_event_reported("user.created") do
  Rails.event.notify("user.created", { id: 123 })
end

To test further details about the reported event, you can specify payload and tag matchers.

assert_event_reported("user.created",
  payload: { id: 123, name: "John Doe" },
  tags: { request_id: /[0-9]+/ }
) do
  Rails.event.tagged(request_id: "123") do
    Rails.event.notify("user.created", { id: 123, name: "John Doe" })
  end
end

The matchers support partial matching - only the specified keys need to match.

assert_event_reported("user.created", payload: { id: 123 }) do
  Rails.event.notify("user.created", { id: 123, name: "John Doe" })
end