Repository: rails/rails
File: activesupport/test/testing/event_reporter_assertions_test.rb (Ruby)

Code before:
            @reporter.notify("another.event", { id: 123, name: "John Doe" })
          end
        end

        assert_match(/Expected an event to be reported matching:/, e.message)
        assert_match(/name: user\.created/, e.message)
        assert_match(/but none of the 1 reported events matched:/, e.message)
        assert_match(/another\.event/, e.message)
      end

      test "#assert_no_event_reported fails when event is reported" do
        payload = { id: 123, name: "John Doe" }
        e = assert_raises(Minitest::Assertion) do
          assert_no_event_reported("user.created") do
            @reporter.notify("user.created", payload)
          end
        end

        assert_match(/Expected no 'user\.created' event to be reported, but found:/, e.message)
        assert_match(/user\.created/, e.message)
      end

      test "assert_multiple_events_reported passes when expecting empty array and no events reported" do
        assert_multiple_events_reported([]) do
          # No events reported
        end
      end

      test "assert_multiple_events_reported" do
        assert_multiple_events_reported([
          { name: "user.created" },
          { name: "email.sent" }
        ]) do
          @reporter.notify("user.created", { id: 123 })
          @reporter.notify("email.sent", { to: "user@example.com" })
        end
      end

      test "assert_multiple_events_reported is order agnostic" do
        assert_multiple_events_reported([
          { name: "user.created", payload: { id: 123 } },
          { name: "email.sent" }
        ]) do
          @reporter.notify("email.sent", { to: "user@example.com" })
          @reporter.notify("user.created", { id: 123, name: "John" })
        end
      end

      test "assert_multiple_events_reported ignores extra events" do
        assert_multiple_events_reported([
          { name: "user.created", payload: { id: 123 } }

Review comment (suggestion):
Can you instead assert that there was no notifier? Otherwise if you can't, I think this test should be deleted.

Code after:
            @reporter.notify("another.event", { id: 123, name: "John Doe" })
          end
        end

        assert_match(/Expected an event to be reported matching:/, e.message)
        assert_match(/name: user\.created/, e.message)
        assert_match(/but none of the 1 reported events matched:/, e.message)
        assert_match(/another\.event/, e.message)
      end

      test "#assert_no_event_reported fails when event is reported" do
        payload = { id: 123, name: "John Doe" }
        e = assert_raises(Minitest::Assertion) do
          assert_no_event_reported("user.created") do
            @reporter.notify("user.created", payload)
          end
        end

        assert_match(/Expected no 'user\.created' event to be reported, but found:/, e.message)
        assert_match(/user\.created/, e.message)
      end

      test "assert_events_reported" do
        assert_events_reported([
          { name: "user.created" },
          { name: "email.sent" }
        ]) do
          @reporter.notify("user.created", { id: 123 })
          @reporter.notify("email.sent", { to: "user@example.com" })
        end
      end

      test "assert_events_reported is order agnostic" do
        assert_events_reported([
          { name: "user.created", payload: { id: 123 } },
          { name: "email.sent" }
        ]) do
          @reporter.notify("email.sent", { to: "user@example.com" })
          @reporter.notify("user.created", { id: 123, name: "John" })
        end
      end

      test "assert_events_reported ignores extra events" do
        assert_events_reported([
          { name: "user.created", payload: { id: 123 } }
        ]) do
          @reporter.notify("extra_event_1")
          @reporter.notify("user.created", { id: 123, name: "John" })
          @reporter.notify("extra_event_2")
          @reporter.notify("extra_event_3")
        end