pub fn verify_that_code<'a, S>(code: S) -> Spec<'a, Code<S>, CollectFailures>where
S: FnOnce(),
Expand description
Starts an assertion for some piece of code in the CollectFailures
mode.
It takes a closure and wraps it into a Spec
. On the Spec
any
assertion method that is implemented for closures can be called.
Assertions started with verify_that_code()
will collect AssertFailure
s
for all failing assertions. The collected failures can be queried by calling
one of the methods failures
or
display_failures
on the Spec
.
In comparison to using the macro verify_that_code!
calling this function does not set a name for the expression and does not
set the code location of the assertion. In failure messages the generic word
“the closure” is used. To set a specific text for the expression the method
named
must be called explicitly.
§Examples
use asserting::prelude::*;
fn divide(a: i32, b: i32) -> i32 {
a / b
}
let failures = verify_that_code(|| { divide(7, 3); })
.does_not_panic()
.failures();
assert_that!(failures).is_empty();
let failures = verify_that_code(|| { divide(7, 0); })
.does_not_panic()
.display_failures();
assert_that!(failures).contains_exactly([
r#"assertion failed: expected the closure to not panic, but did panic
with message: "attempt to divide by zero"
"#
]);
let failures = verify_that_code(|| { divide(7, 0); })
.panics_with_message("division by zero")
.display_failures();
assert_that!(failures).contains_exactly([
r#"assertion failed: expected the closure to panic with message "division by zero"
but was: "attempt to divide by zero"
expected: "division by zero"
"#
]);