🛈 Note: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem.

For the release documentation, please see docs.rs, instead.

tracing_mock

Module field

Source
Expand description

Define expectations to validate fields on events and spans.

The ExpectedField struct define expected values for fields in order to match events and spans via the mock collector API in the collector module.

Expected fields should be created with expect::field and a chain of method calls to specify the field value and additional fields as necessary.

§Examples

The simplest case is to expect that an event has a field with a specific name, without any expectation about the value:

use tracing_mock::{collector, expect};

let event = expect::event()
    .with_fields(expect::field("field_name"));

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

tracing::collect::with_default(collector, || {
    tracing::info!(field_name = "value");
});

handle.assert_finished();

It is possible to expect multiple fields and specify the value for each of them:

use tracing_mock::{collector, expect};

let event = expect::event().with_fields(
    expect::field("string_field")
        .with_value(&"field_value")
        .and(expect::field("integer_field").with_value(&54_i64))
        .and(expect::field("bool_field").with_value(&true)),
);

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

tracing::collect::with_default(collector, || {
    tracing::info!(
        string_field = "field_value",
        integer_field = 54_i64,
        bool_field = true,
    );
});

handle.assert_finished();

If an expected field is not present, or if the value of the field is different, the test will fail. In this example, the value is different:

use tracing_mock::{collector, expect};

let event = expect::event()
    .with_fields(expect::field("field_name").with_value(&"value"));

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

tracing::collect::with_default(collector, || {
    tracing::info!(field_name = "different value");
});

handle.assert_finished();

Structs§