1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-08-02 18:55:05 +00:00

add test builder guide information

This commit is contained in:
Nikolay Kim 2018-03-29 19:22:43 -07:00
parent 145010a2b0
commit d80b84c915

View file

@ -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<PParam>| 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