Skip to content

Commit

Permalink
add custom response test
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenArcher committed Feb 21, 2024
1 parent 7aa6c2a commit 3370bed
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/tests/response.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use actix_web::{http::StatusCode, web, ResponseError};
use serde::Serialize;

use crate::response::{AppError, AppResponse, AppResult};
use crate::response::{AppError, AppResponse, AppResult, OkModel};

#[test]
fn success() {
Expand Down Expand Up @@ -33,3 +34,26 @@ fn internal_server_error() {
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
}

#[test]
fn custom_response() {
#[derive(Serialize)]
struct RespBody {
a: i32,
b: f64,
msg: &'static str,
}
let web::Json(model) = AppResponse::Success(RespBody {
a: 1,
b: 3.1415,
msg: "Hello!",
})
.response()
.unwrap();
let OkModel { success, data } = model;
assert!(success);
let RespBody { a, b, msg } = data;
assert_eq!(a, 1);
assert_ne!(b, 9876.5432);
assert_eq!(msg, "Hello!");
}

0 comments on commit 3370bed

Please sign in to comment.