pub async fn assert_status_json<StructType, Status>(
    res: impl AsMut<Response>,
    status: Status
) -> StructType where
    StructType: DeserializeOwned,
    Status: TryInto<StatusCode>,
    Status::Error: Debug
Expand description

Assert that a response has a status code and parse out the body to JSON if possible.

This helper has better assertion failure messages than doing this manually.

Example:

use preroll::test_utils::{self, assert_status_json, TestResult};
use preroll::JsonError;

pub fn setup_routes(mut server: tide::Route<'_, std::sync::Arc<()>>) {
  // Normally imported from your service's crate (lib.rs).
}

#[async_std::main] // Would be #[async_std::test] instead.
async fn main() -> TestResult<()> {
    let client = test_utils::create_client((), setup_routes).await.unwrap();

    let mut res = client.get("/not_found").await.unwrap();

    let json: JsonError = assert_status_json(&mut res, 404).await;
    assert_eq!(&json.title, res.status().canonical_reason());

    Ok(())
}