1
0
mirror of https://github.com/actix/actix-website synced 2024-11-30 19:14:36 +01:00
actix-website/docs/testing.md
Santiago 8393aea71a
migrate to docusaurus (v2) (#266)
Co-authored-by: ibraheemdev <ibrah1440@gmail.com>
2022-07-16 18:59:20 +09:00

2.1 KiB

title
Testing

import CodeBlock from "@site/src/components/code_block.js";

Testing

Every application should be well tested. Actix Web provides tools to perform unit and integration tests.

Unit Tests

For unit testing, actix-web provides a request builder type. TestRequest implements a builder-like pattern. You can generate a HttpRequest instance with to_http_request() and call your handler with it.

Integration tests

There are a few methods for testing your application. Actix Web can be used to run the application with specific handlers in a real HTTP server.

TestRequest::get(), TestRequest::post() and other methods can be used to send requests to the test server.

To create a Service for testing, use the test::init_service method which accepts a regular App builder.

Check the API documentation for more information.

If you need more complex application configuration, testing should be very similar to creating the normal application. For example, you may need to initialize application state. Create an App with a data method and attach state just like you would from a normal application.

Stream response tests

If you need to test stream generation, it would be enough to call take_body() and convert a resulting ResponseBody into a future and execute it, for example when testing Server Sent Events.