本页尚未翻译
下面显示的是英文原文。
broker
TestApp::broker addresses one broker of the app by its production type, for publishing to it and reading its records.
B is the type the app registered, so MemoryBroker and MemoryBroker<Retaining> are two different brokers.
The BrokerHandle it returns publishes onto that broker with message(&v) and opens its records: subscriber(name) and published::<T>(name).
A subscription name is local to its broker: the "orders" of one broker is not the "orders" of the other.
broker panics when the app has no broker of type B or more than one; two brokers of one type are addressed by label with broker_named.
In live mode the test's input reaches the broker through its connected form's default publish policy.
Example
- Macros
- Manual
#[tokio::test]
async fn each_broker_is_addressed_by_its_type() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<MemoryBroker<Retaining>>()
.message(&Order { id: 7 })
.publish()
.await?;
tb.broker::<MemoryBroker<Retaining>>()
.subscriber("orders")
.assert_called_once();
tb.broker::<MemoryBroker>()
.subscriber("orders")
.assert_not_called();
Ok(())
}
#[tokio::test]
async fn each_broker_is_addressed_by_its_type() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<MemoryBroker<Retaining>>()
.message(&Order { id: 7 })
.publish()
.await?;
tb.broker::<MemoryBroker<Retaining>>()
.subscriber("orders")
.assert_called_once();
tb.broker::<MemoryBroker>()
.subscriber("orders")
.assert_not_called();
Ok(())
}