Skip to content

Instantly share code, notes, and snippets.

@AndyWatt83
Created February 12, 2022 23:43
Show Gist options
  • Save AndyWatt83/a30351bb6ca7ae597fad5a12245520ab to your computer and use it in GitHub Desktop.
Save AndyWatt83/a30351bb6ca7ae597fad5a12245520ab to your computer and use it in GitHub Desktop.
Testing for a very simple Rust API
#[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