Created
February 12, 2022 23:43
-
-
Save AndyWatt83/a30351bb6ca7ae597fad5a12245520ab to your computer and use it in GitHub Desktop.
Testing for a very simple Rust API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[cfg(test)] | |
mod tests { | |
use super::*; | |
use actix_web::body::to_bytes; | |
use actix_web::dev::Service; | |
use actix_web::{http, test, web, App, Error}; | |
#[actix_web::test] | |
async fn test_say_hello() -> Result<(), Error> { | |
let app = App::new().route("/", web::get().to(say_hello)); | |
let app = test::init_service(app).await; | |
let req = test::TestRequest::get().uri("/").to_request(); | |
let resp = app.call(req).await.unwrap(); | |
assert_eq!(resp.status(), http::StatusCode::OK); | |
let response_body = resp.into_body(); | |
assert_eq!(to_bytes(response_body).await.unwrap(), r##"Hello world!"##); | |
Ok(()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment