Skip to content

Commit

Permalink
Create dashboard_tests.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 8, 2024
1 parent c32461c commit 195f4cc
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/dashboard/dashboard_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#[cfg(test)]
mod tests {
use super::*;
use actix_web::{test, App};

#[actix_web::test]
async fn test_index() {
let state = AppState {
user_count: Mutex::new(5),
};

let app = test::init_service(App::new().data(state).route("/", web::get().to(index))).await;
let req = test::TestRequest::get().uri("/").to_request();
let resp: serde_json::Value = test::read_response_json(&app, req).await;

assert_eq!(resp["message"], "Welcome to the Dashboard!");
assert_eq!(resp["user_count"], 5);
}

#[actix_web::test]
async fn test_increment_user_count() {
let state = AppState {
user_count: Mutex::new(5),
};

let app = test::init_service(App::new().data(state).route("/increment", web::post().to(increment_user_count))).await;
let req = test::TestRequest::post().uri("/increment").to_request();
let resp: serde_json::Value = test::read_response_json(&app, req).await;

assert_eq!(resp["user_count"], 6);
}
}

0 comments on commit 195f4cc

Please sign in to comment.