1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 07:29:02 +02:00

update testing docs to give less precedence to unit testing

This commit is contained in:
Rob Ede
2023-09-10 02:56:48 +01:00
parent e67443f4e8
commit c5a9579193
6 changed files with 22 additions and 18 deletions

View File

@ -6,15 +6,11 @@ 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.
Every application should be well tested. Actix Web provides tools to perform integration tests against your applications and unit test tools for custom extractors and middleware.
## Unit Tests
Actix Web provides a request builder type. [_TestRequest_][testrequest] implements a builder-like pattern. You can generate a `HttpRequest` instance with `to_http_request()` and call your handlers or extractors with it. Also see
For unit testing, actix-web provides a request builder type. [_TestRequest_][testrequest] implements a builder-like pattern. You can generate a `HttpRequest` instance with `to_http_request()` and call your handler with it.
<CodeBlock example="testing" file="main.rs" section="unit-tests" />
## Integration tests
## Integration Testing For Applications
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.
@ -30,12 +26,18 @@ If you need more complex application configuration, testing should be very simil
<CodeBlock example="testing" file="integration_two.rs" section="integration-two" />
## Stream response tests
## Stream Response Testing
If you need to test stream generation, it would be enough to call [`into_parts()`][resintoparts] and convert the resulting body into a future and execute it, for example when testing [_Server Sent Events_][serversentevents].
<CodeBlock example="testing" file="stream_response.rs" section="stream-response" />
## Unit Testing Extractors
Unit testing has pretty limited value for applications, but can be useful when developing extractors, middleware, and responders. Given that, calling directly into handler functions **which are defined stand-alone, without using routing macros** (like `#[get("/")]`) is possible if you want to make assertions on custom `Responder`s.
<CodeBlock example="testing" file="main.rs" section="unit-tests" />
[serversentevents]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
[resintoparts]: https://docs.rs/actix-web/4/actix_web/struct.HttpResponse.html#method.into_parts
[actixdocs]: https://docs.rs/actix-web/4/actix_web/test/index.html