Added first unit tests

This commit is contained in:
Jef Roosens 2021-03-05 23:34:38 +01:00
parent e498fb38c8
commit 28aab01e77
Signed by: Jef Roosens
GPG key ID: B580B976584B5F30
7 changed files with 49 additions and 19 deletions

View file

@ -1 +1,16 @@
pub mod routes;
#[cfg(test)] mod tests;
#[get("/world")]
pub fn world() -> &'static str {
"Hello, world!"
}
#[get("/<name>")]
pub fn hello(name: String) -> String {
format!("Hello, {}", name)
}
#[get("/world?<name>&<age>")]
pub fn name_age(name: String, age: u16) -> String {
format!("Hello, {} who is {} years old!", name, age)
}

View file

@ -1,14 +0,0 @@
#[get("/world")]
pub fn world() -> &'static str {
"Hello, world!"
}
#[get("/<name>")]
pub fn hello(name: String) -> String {
format!("Hello, {}", name)
}
#[get("/world?<name>&<age>")]
pub fn name_age(name: String, age: u16) -> String {
format!("Hello, {} who is {} years old!", name, age)
}

24
src/hello/tests.rs Normal file
View file

@ -0,0 +1,24 @@
use rocket::local::Client;
use rocket::http::Status;
fn rocket() -> rocket::Rocket {
rocket::ignite().mount("/", routes![super::world, super::hello, super::name_age])
}
#[test]
fn test_world() {
let client = Client::new(rocket()).expect("valid rocket instance");
let mut response = client.get("/world").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.body_string(), Some("Hello, world!".into()));
}
#[test]
fn test_hello() {
let client = Client::new(rocket()).expect("valid rocket instance");
let mut response = client.get("/thisisaname").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.body_string(), Some("Hello, thisisaname".into()));
}