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

下面显示的是英文原文。

Shortcuts

TestApp::publish and BrokerHandle::publish send any serde value to a named destination in one call; publish_with_headers adds headers.

They are for a value whose type the test does not own, such as a serde_json::Value. The builder of message(&v) needs the type to declare a destination, and the orphan rule keeps a foreign type from declaring one. The value is encoded with the default codec and sent to name; the call then drives the reaction like publish(). publish_with_headers serializes a typed header contract into the delivery's headers, the input a Message<H, P> or Headers<H> handler reads. TestApp::publish needs a single broker and fails with TestError::Ambiguous otherwise. A value that does not encode fails the call with TestError::Encode.

Example​

#[tokio::test]
async fn any_serde_value_can_be_published() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.publish("orders", &json!({ "id": 7 })).await?;
tb.broker::<MemoryBroker>()
.publish_with_headers(
"tenant-orders",
&json!({ "id": 8 }),
&Tenant {
tenant: "acme".to_owned(),
},
)
.await?;

let broker = tb.broker::<MemoryBroker>();
broker
.subscriber("orders")
.assert_called_once()
.with(&Order { id: 7 });
broker
.subscriber("tenant-orders")
.assert_called_once()
.settled(HandlerOutcome::ack());
Ok(())
}