assert_called_once
assert_called_once checks that a subscriber's handler ran exactly once: one delivery, or one batch.
It counts handler calls in the record, subscriber(name), not messages: a batch handler called with five orders counts once.
A delivery that did not decode counts too: the record holds it although the body never ran.
It returns the assertions, so with, with_raw and settled chain after it and read that one call.
On failure it panics with the number of calls it found.
assert_called(n) checks any other number, and assert_not_called checks that there was no call.
Example
- Macros
- Manual
#[tokio::test]
async fn the_handler_ran_once() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<MemoryBroker>()
.message(&Order { id: 7 })
.to("orders")
.publish()
.await?;
tb.broker::<MemoryBroker>()
.subscriber("orders")
.assert_called_once();
Ok(())
}
#[tokio::test]
async fn the_handler_ran_once() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<MemoryBroker>()
.message(&Order { id: 7 })
.to("orders")
.publish()
.await?;
tb.broker::<MemoryBroker>()
.subscriber("orders")
.assert_called_once();
Ok(())
}