Macro restest::assert_body_matches[][src]

assert_body_matches!() { /* proc-macro */ }
Expand description

Asserts that a response body matches a given pattern, adds bindings to the current scope.

This pattern supports all the Rust pattern syntax, with a few additions:

  • matching on String can be done with string literals,
  • matching on Vec can be done using slice patterns,
  • values that are bound to variables are available in the whole scope, allowing for later use.

Panics

This macro will panic if the body does not match the provided pattern.

Example

The following code demonstrate basic matching:

use restest::assert_body_matches;

struct User {
    name: String,
    age: u8,
}

let user = get_user();

assert_body_matches! {
    user,
    User {
        name: "John Doe",
        age: 48,
    },
}

fn get_user() -> User {
    /* Obscure code */
}

Values can be brought to scope:

use restest::assert_body_matches;
use uuid::Uuid;

struct User {
    id: Uuid,
    name: String,
}

let user = get_user();

assert_body_matches! {
    user,
    User {
        id,
        name: "John Doe",
    },
}

// id is now available:
println!("John Doe has id `{}`", id);

fn get_user() -> User {
    /* Obscure code */
}

Bringing values to scope may allow to extract information that are required to perform a next request.