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

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

Function tracing_mock::collector::mock

source ·
pub fn mock() -> MockCollector<fn(_: &Metadata<'_>) -> bool>
Expand description

Create a new MockCollector.

For additional information and examples, see the collector module and MockCollector documentation.

§Examples

use tracing_mock::{collector, expect, field};

let span = expect::span()
    .named("my_span");
let (collector, handle) = collector::mock()
    // Enter a matching span
    .enter(span.clone())
    // Record an event with message "collect parting message"
    .event(expect::event().with_fields(expect::message("collect parting message")))
    // Record a value for the field `parting` on a matching span
    .record(span.clone(), expect::field("parting").with_value(&"goodbye world!"))
    // Exit a matching span
    .exit(span)
    // Expect no further messages to be recorded
    .only()
    // Return the collector and handle
    .run_with_handle();

// Use `with_default` to apply the `MockCollector` for the duration
// of the closure - this is what we are testing.
tracing::collect::with_default(collector, || {
    let span = tracing::trace_span!(
        "my_span",
        greeting = "hello world",
        parting = tracing::field::Empty
    );

    let _guard = span.enter();
    tracing::info!("collect parting message");
    let parting = "goodbye world!";

    span.record("parting", &parting);
});

// Use the handle to check the assertions. This line will panic if an
// assertion is not met.
handle.assert_finished();