Added first unit tests

master
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

@ -23,4 +23,10 @@ image: Dockerfile
# Run # Run
run: run:
@ cargo run @ cargo run
.PHONY: run
# Testing
test:
@ cargo test
.PHONY: test

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 100644
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()));
}

View File

@ -1 +0,0 @@
mod ivago.controller;

View File

@ -6,8 +6,8 @@ mod hello;
fn main() { fn main() {
rocket::ignite().mount("/hello", routes![ rocket::ignite().mount("/hello", routes![
hello::routes::world, hello::world,
hello::routes::hello, hello::hello,
hello::routes::name_age hello::name_age
]).launch(); ]).launch();
} }