Giter VIP home page Giter VIP logo

Comments (3)

robjtede avatar robjtede commented on May 26, 2024 1

"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.

dimvoly avatar dimvoly commented on May 26, 2024 1

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.

robjtede avatar robjtede commented on May 26, 2024

reworked some wording in c5a9579

  • less precedence put on unit tests
  • integration tests use routing macro setups

from actix-website.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.