跳到主要内容
本页尚未翻译

下面显示的是英文原文。

on_publish

on_publish(hook) runs an async closure each time a message is published to a destination, handing it that destination's assertions as they stood at that publish.

It sees the test's own publishes and everything a publisher wired by the runtime sends: replies, Out slot publishes, retry copies and dead letters. On the record of a slot, out::<M>(), it runs for each publish through that slot. The hook gets PublishedAssertions with the new message already in the record and the later ones left out, so decoded and with work inside it. The record is the message as the framework handed it to the broker, in process and against a running stand alike. The publish the test made returns only after the hook finished, and a failed assertion inside the hook fails it, or else the test's shutdown or the end of the test. A hook applies from the moment it is registered, and several hooks on one destination run in the order they were registered.

Example​

#[tokio::test]
async fn every_confirmation_reaches_the_audit() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
let (audit, mut audited) = mpsc::channel(8);
tb.broker::<MemoryBroker>()
.published::<Confirmation>("confirmations")
.on_publish(async move |confirmations| {
let last = confirmations.decoded().pop();
let _ = audit.send(last).await;
});

tb.broker::<MemoryBroker>()
.message(&Order { id: 7 })
.publish()
.await?;

assert_eq!(
audited.recv().await.flatten(),
Some(Confirmation { order_id: 7 })
);
Ok(())
}