pub struct ExpectedFields { /* private fields */ }
Expand description
An expectation for multiple fields.
For a detailed description and examples, see the documentation for
the methods and the field
module.
Implementations§
Source§impl ExpectedFields
impl ExpectedFields
Sourcepub fn and(self, field: ExpectedField) -> Self
pub fn and(self, field: ExpectedField) -> Self
Adds an additional ExpectedField
to be matched.
All fields must match, if any of them are not present, or if the value for any field is different, the expectation will fail.
This method performs the same function as
ExpectedField::and
, but applies in the case where there are
already multiple fields expected.
§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))
.and(expect::field("a_third_field").with_value(&true)),
);
let (collector, handle) = collector::mock()
.event(event)
.run_with_handle();
tracing::collect::with_default(collector, || {
tracing::info!(
field = "value",
another_field = 42,
a_third_field = true,
);
});
handle.assert_finished();
If any of the expected fields are not present on the recorded event, 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))
.and(expect::field("a_third_field").with_value(&true)),
);
let (collector, handle) = collector::mock()
.event(event)
.run_with_handle();
tracing::collect::with_default(collector, || {
tracing::info!(
field = "value",
a_third_field = true,
);
});
handle.assert_finished();
Sourcepub fn only(self) -> Self
pub fn only(self) -> Self
Indicates that no fields other than those specified should be expected.
This method performs the same function as
ExpectedField::only
, but applies in the case where there are
multiple fields expected.
§Examples
The following test will pass, even though additional fields are recorded on the event.
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,
a_third_field = true,
);
});
handle.assert_finished();
If we include only
on the ExpectedFields
then 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))
.only(),
);
let (collector, handle) = collector::mock()
.event(event)
.run_with_handle();
tracing::collect::with_default(collector, || {
tracing::info!(
field = "value",
another_field = 42,
a_third_field = true,
);
});
handle.assert_finished();