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

下面显示的是英文原文。

with_state

TestApp::with_state starts the app with a state the test builds in place of on_startup, wired to the brokers of the app.

It is for a state that reaches outside the service: a catalog, a database, a mail client the test cannot or should not call. The harness connects each broker in process first, then calls the builder, so the state's publishers pair against connected brokers. TestBrokers::broker::<B>() returns the connected form of the app's broker of type B. A publisher built from it publishes onto the bus the assertions read, such as published::<T>(name) and its with. The app's on_startup does not run; after_startup and everything else of startup do, as in TestApp::start. TestBrokers::broker panics when the app has no broker of type B or more than one.

Example​

#[tokio::test]
async fn an_order_is_quoted_from_the_catalog() -> Result<(), Box<dyn Error>> {
let tb = TestApp::with_state(app(), |brokers| Pricing {
catalog: Catalog {
prices: HashMap::from([(7, 1250)]),
},
quotes: brokers.broker::<MemoryBroker>().publisher(),
})
.await?;
tb.broker::<MemoryBroker>()
.message(&Order { id: 7 })
.publish()
.await?;

tb.broker::<MemoryBroker>()
.published::<Quote>("quotes")
.assert_called_once()
.with(&Quote {
order_id: 7,
cents: 1250,
});
Ok(())
}