Comments (3)
"unit tests" cannot be used to test handlers that use macro-based routing.
I recommend always using "integration tests" for handlers. Will leave this issue open since it's not clear in the docs.
from actix-website.
I've struggled with this when reading the tutorial.
It starts us off with the attribute macro that specifies the url:
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
And to build the app you do this:
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(hello)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
I've then skipped ahead to the testing section where you have unit tests and integration tests. Per searching around the internet (and this issue), unit tests are not suitable for use with the attribute macro.
However, neither is the current integration code snippet in the docs. You have to change the way the app is built up:
#[cfg(test)]
mod tests {
use actix_web::{http::header::ContentType, test, web, App};
use super::*;
#[actix_web::test]
async fn test_index_get() {
let app = test::init_service(App::new().service(index)).await;
let req = test::TestRequest::default()
.insert_header(ContentType::plaintext())
.to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_success());
}
}
Notice we call .service(index)
, instead of .route("/", web::get().to(index))
.
I do like the routing attribute macros, as I'm coming over from Flask.
from actix-website.
reworked some wording in c5a9579
- less precedence put on unit tests
- integration tests use routing macro setups
from actix-website.
Related Issues (20)
- Can't compile either static file example HOT 2
- Getting started: Indicate that the example code should be written in src/main.rs
- HTTP/2 support works with rustls, docs may be outdated HOT 1
- please preserve my eyesight and add a dark mode HOT 1
- Dangerous tip in Static Files
- ^^ the trait `Responder` is not implemented for `NamedFile` HOT 1
- Broken links HOT 3
- request monospace font HOT 2
- Outdated info in Server section HOT 1
- ACTIX WEB HTTP2 & OPENSSL : not working exact example from actix.rs website HOT 2
- invalid link to match_info in docs HOT 2
- Actix book link broken HOT 1
- Invalid Link in Extractors Tab (in Docs)
- Chunked body request example does not render on website HOT 1
- The `connection_type` method should not be called on `HttpResponseBuilder` instance. HOT 1
- Search functionality HOT 1
- Add more complex middleware example
- update docusaurus to v3
- Rendering issue with `warning` fence in `static-files.md`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from actix-website.