By default, it patches your broker to run In-Memory, so you can use it without any external broker. It should be extremely useful in your CI or local development environment.
Also, it allows you to check the outgoing message body in the same way as with a subscriber.
publisher.mock.assert_called_once_with("Hi!")
In addition, the publisher has the same assert_called_once_with, assert_called_with and assert_any_call methods as a subscriber. They take the body as a dict, your model or a matcher, and the message fields the outgoing message carried. Here the publisher inherits the correlation_id of the message the handler consumed:
The Publisher mock contains not just a publish method input value. It sets up a virtual consumer for an outgoing topic, consumes a message, and stores this consumed one.
Note
In order for publishers to be properly patched by the test broker, you need to create them before running the test broker
Additionally, TestBroker can be used with a real external broker to make your tests end-to-end suitable. For more information, please visit the subscriber testing page.
If a publisher is triggered from a lifespan hook rather than from a subscriber, the hooks have to run inside the test as well. TestApp does that: see Events Testing.