1
0
mirror of https://github.com/actix/actix-website synced 2024-11-24 00:41:07 +01:00
actix-website/content/docs/testing.md

70 lines
3.0 KiB
Markdown
Raw Normal View History

2018-05-22 23:15:08 +02:00
---
title: Testing
menu: docs_advanced
weight: 210
---
# Testing
Every application should be well tested. Actix provides tools to perform unit and
integration tests.
# Unit Tests
For unit testing, actix provides a request builder type and a simple handler runner.
[*TestRequest*](../../actix-web/actix_web/test/struct.TestRequest.html)
implements a builder-like pattern.
2019-06-17 21:39:58 +02:00
You can generate a `HttpRequest` instance with `to_http_request()`, or you can
run your handler with `block_on()`.
{{< include-example example="testing" file="main.rs" section="unit-tests" >}}
2018-05-22 23:15:08 +02:00
# Integration tests
There are several methods for testing your application. Actix provides
[*TestServer*](../../actix-web/actix_web/test/struct.TestServer.html), which can be used
to run the application with specific handlers in a real http server.
`TestServer::get()`, `TestServer::post()`, and `TestServer::client()`
methods can be used to send requests to the test server.
A simple form `TestServer` can be configured to use a handler.
`TestServer::new` method accepts a configuration function, and the only argument
for this function is a *test application* instance.
> Check the [api documentation](../../actix-web/actix_web/test/struct.TestApp.html)
> for more information.
2019-06-17 21:39:58 +02:00
{{< include-example example="testing" file="integration_one.rs" section="integration-one" >}}
2018-05-22 23:15:08 +02:00
The other option is to use an application factory. In this case, you need to pass the factory
function the same way as you would for real http server configuration.
2019-06-17 21:39:58 +02:00
{{< include-example example="testing" file="integration_two.rs" section="integration-two" >}}
2018-05-22 23:15:08 +02:00
If you need more complex application configuration, use the `TestServer::build_with_state()`
method. For example, you may need to initialize application state or start `SyncActor`'s for diesel
interation. This method accepts a closure that constructs the application state,
and it runs when the actix system is configured. Thus, you can initialize any additional actors.
2019-06-17 21:39:58 +02:00
{{< include-example example="testing" file="integration_three.rs" section="integration-three" >}}
2018-05-23 22:35:42 +02:00
# Stream response tests
If you need to test stream it would be enough to convert a [*ClientResponse*](../../actix-web/actix_web/client/struct.ClientResponse.html) to future and execute it.
For example of testing [*Server Sent Events*](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events).
2019-06-17 21:39:58 +02:00
{{< include-example example="testing" file="stream_response.rs" section="stream-response" >}}
2018-05-23 22:35:42 +02:00
2018-05-22 23:15:08 +02:00
# WebSocket server tests
It is possible to register a *handler* with `TestApp::handler()`, which
initiates a web socket connection. *TestServer* provides the method `ws()`, which connects to
the websocket server and returns ws reader and writer objects. *TestServer* also
provides an `execute()` method, which runs future objects to completion and returns
result of the future computation.
The following example demonstrates how to test a websocket handler:
2019-06-17 21:39:58 +02:00
{{< include-example example="testing" file="websockets.rs" section="web-socket" >}}