assert_that_code

Function assert_that_code 

Source
pub fn assert_that_code<'a, S>(code: S) -> Spec<'a, Code<S>, PanicOnFail>
where S: FnOnce(),
Available on crate feature panic only.
Expand description

Starts an assertion for some piece of code in the PanicOnFail 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 assert_that_code() will panic on the first failing assertion.

In comparison to using the macro assert_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
}

assert_that_code(|| { divide(7, 0); })
    .panics_with_message("attempt to divide by zero");

assert_that_code(|| { divide(7, 3); }).does_not_panic();