Testing Actix Web Endpoints: Troubleshooting Errors

36 views Asked by At

I'm testing a component in my Actix-web application that echoes back the request body and query parameters in the response. Here's how I'm testing the delete_responder endpoint:

#[test]
async fn test_delete_responder_with_values_and_body_data() {
    // Initialize the service with the delete_responder
    let mut app = test::init_service(App::new().service(delete_responder)).await;
    let payload = r#"{"id":"12345","name":"User name"}"#.as_bytes();

    // Mock DELETE request to /delete endpoint with body data and query parameters
    let req = test::TestRequest::delete()
        .uri("/delete?id=123&message=test_message")
        .set_payload(payload)
        .to_request();

    // Send the request to the service
    let resp: Person = test::call_and_read_body_json(&mut app, req).await;
    println!("Response: {:?}", resp);

    // Assert that the response contains the expected values
    assert_eq!(resp.id, "12345".to_string());
    assert_eq!(resp.name, "User name".to_string());
}

However, I'm encountering errors and I'm not sure what might be causing them. My Person struct is the type of the response body.

Person Struct.

#[derive(Debug, Serialize, Deserialize)]
pub struct Person {
    id: String,
    name: String,
}

Could you help me identify what might be causing the errors?

---- delete::tests::test_delete_responder_with_values_and_body_data stdout ----
thread 'delete::tests::test_delete_responder_with_values_and_body_data' panicked at /Users/.../test_utils.rs:328:49:
called `Result::unwrap()` on an `Err` value: Error("missing field `id`", line: 1, column: 168)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    delete::tests::test_delete_responder_with_values_and_body_data

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

0

There are 0 answers