From d80b84c9155bf3ed703288f306696a9d8e104967 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 29 Mar 2018 19:22:43 -0700 Subject: [PATCH] add test builder guide information --- guide/src/qs_8.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/guide/src/qs_8.md b/guide/src/qs_8.md index d8c96d811..f6ec722d5 100644 --- a/guide/src/qs_8.md +++ b/guide/src/qs_8.md @@ -103,6 +103,33 @@ fn main() { } ``` +If you need more complex application configuration, for example you may need to +initialize application state or start `SyncActor`'s for diesel interation, you +can use `TestServer::build_with_state()` method. This method accepts closure +that has to construct application state. This closure runs when actix system is +configured already, so you can initialize any additional actors. + +```rust,ignore +#[test] +fn test() { + let srv = TestServer::build_with_state(|| { // <- construct builder with config closure + // we can start diesel actors + let addr = SyncArbiter::start(3, || { + DbExecutor(SqliteConnection::establish("test.db").unwrap()) + }); + // then we can construct custom state, or it could be `()` + MyState{addr: addr} + }) + .start(|app| { // <- register server handlers and start test server + app.resource( + "/{username}/index.html", |r| r.with( + |p: Path| format!("Welcome {}!", p.username))); + }); + + // now we can run our test code +); +``` + ## WebSocket server tests It is possible to register a *handler* with `TestApp::handler()` that