Repository: rails/rails
File: activesupport/lib/active_support/testing/event_reporter_assertions.rb (Ruby)

Code before:
      # 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
      def assert_event_reported(name, payload: nil, tags: {}, &block)
        events = EventCollector.record(&block)

        if events.empty?
          flunk("Expected an event to be reported, but there were no events reported.")
        elsif (event = events.find { |event| event.matches?(name, payload, tags) })
          assert(true)
          event.event_data
        else
          message = "Expected an event to be reported matching:\n  " \
            "name: #{name}\n  " \
            "payload: #{payload.inspect}\n  " \
            "tags: #{tags.inspect}\n" \
            "but none of the #{events.size} reported events matched:\n  " \
            "#{events.map(&:inspect).join("\n  ")}"
          flunk(message)
        end
      end

      # Asserts that the block causes the provided events to be reported,
      # regardless of order.
      #
      #   assert_multiple_events_reported([
      #     { name: "user.created", payload: { id: 123 } },
      #     { name: "email.sent", payload: { to: "user@example.com" } }
      #   ]) do
      #     create_user_and_send_welcome_email
      #   end
      #
      # Supports the same payload and tag matching as +assert_event_reported+.
      #
      #   assert_multiple_events_reported([
      #     {
      #       name: "process.started",
      #       payload: { id: 123 },
      #       tags: { request_id: /[0-9]+/ }
      #     },
      #     { name: "process.completed" }
      #   ]) do
      #     Rails.event.tagged(request_id: "456") do
      #       start_and_complete_process(123)
      #     end
      #   end
      def assert_multiple_events_reported(expected_events, &block)
        events = EventCollector.record(&block)


Review comment (suggestion):
I think this can be said more simply:

```suggestion
      # Asserts that the provided events were reported, regardless of order.
```

Code after:
      # 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
      def assert_event_reported(name, payload: nil, tags: {}, &block)
        events = EventCollector.record(&block)

        if events.empty?
          flunk("Expected an event to be reported, but there were no events reported.")
        elsif (event = events.find { |event| event.matches?(name, payload, tags) })
          assert(true)
          event.event_data
        else
          message = "Expected an event to be reported matching:\n  " \
            "name: #{name}\n  " \
            "payload: #{payload.inspect}\n  " \
            "tags: #{tags.inspect}\n" \
            "but none of the #{events.size} reported events matched:\n  " \
            "#{events.map(&:inspect).join("\n  ")}"
          flunk(message)
        end
      end

      # Asserts that the provided events were reported, regardless of order.
      #
      #   assert_events_reported([
      #     { name: "user.created", payload: { id: 123 } },
      #     { name: "email.sent", payload: { to: "user@example.com" } }
      #   ]) do
      #     create_user_and_send_welcome_email
      #   end
      #
      # Supports the same payload and tag matching as +assert_event_reported+.
      #
      #   assert_events_reported([
      #     {
      #       name: "process.started",
      #       payload: { id: 123 },
      #       tags: { request_id: /[0-9]+/ }
      #     },
      #     { name: "process.completed" }
      #   ]) do
      #     Rails.event.tagged(request_id: "456") do
      #       start_and_complete_process(123)
      #     end
      #   end
      def assert_events_reported(expected_events, &block)
        events = EventCollector.record(&block)

        if events.empty? && expected_events.size > 0