pub struct ExpectedField { /* private fields */ }
Expand description
An expected field.
For a detailed description and examples, see the documentation for
the methods and the field
module.
Implementations§
Source§impl ExpectedField
impl ExpectedField
Sourcepub fn with_value(self, value: &dyn Value) -> Self
pub fn with_value(self, value: &dyn Value) -> Self
Sets the value to expect when matching this field.
If the recorded value for this field is different, the expectation will fail.
§Examples
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 = "value");
});
handle.assert_finished();
A different value will cause the test to fail:
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();
Sourcepub fn and(self, other: ExpectedField) -> ExpectedFields
pub fn and(self, other: ExpectedField) -> ExpectedFields
Adds an additional ExpectedField
to be matched.
Both fields must match, if either of them are not present, or if the value for either field is different, the expectation will fail.
§Examples
use tracing_mock::{collector, expect};
let event = expect::event().with_fields(
expect::field("field")
.with_value(&"value")
.and(expect::field("another_field").with_value(&42)),
);
let (collector, handle) = collector::mock()
.event(event)
.run_with_handle();
tracing::collect::with_default(collector, || {
tracing::info!(
field = "value",
another_field = 42,
);
});
handle.assert_finished();
If the second field is not present, the test will fail:
use tracing_mock::{collector, expect};
let event = expect::event().with_fields(
expect::field("field")
.with_value(&"value")
.and(expect::field("another_field").with_value(&42)),
);
let (collector, handle) = collector::mock()
.event(event)
.run_with_handle();
tracing::collect::with_default(collector, || {
tracing::info!(field = "value");
});
handle.assert_finished();
Sourcepub fn only(self) -> ExpectedFields
pub fn only(self) -> ExpectedFields
Indicates that no fields other than those specified should be expected.
If additional fields are present on the recorded event or span, the expectation will fail.
§Examples
The following test passes despite the recorded event having
fields that were not expected because only
was not
used:
use tracing_mock::{collector, expect};
let event = expect::event()
.with_fields(expect::field("field").with_value(&"value"));
let (collector, handle) = collector::mock().event(event).run_with_handle();
tracing::collect::with_default(collector, || {
tracing::info!(field = "value", another_field = 42,);
});
handle.assert_finished();
If we include only
on the ExpectedField
then the test
will fail:
use tracing_mock::{collector, expect};
let event = expect::event()
.with_fields(expect::field("field").with_value(&"value").only());
let (collector, handle) = collector::mock().event(event).run_with_handle();
tracing::collect::with_default(collector, || {
tracing::info!(field = "value", another_field = 42,);
});
handle.assert_finished();