pub struct Unimock { /* private fields */ }
Expand description
Unimock’s purpose is to be an implementor of downstream traits via mock objects. A single mock object provides the implementation of a single trait method.
The interaction with these mock objects always happen via the Unimock facade and the traits that it implements.
Implementations§
source§impl Unimock
impl Unimock
sourcepub fn new(setup: impl Clause) -> Self
pub fn new(setup: impl Clause) -> Self
Construct a unimock instance which strictly adheres to the description in the passed Clause.
Every call hitting the instance must be declared in advance as a clause, or else panic will ensue.
Example
#[unimock(api=TraitMock)]
trait Trait {
fn foo(&self) -> &'static str;
}
let mocked = Unimock::new(TraitMock::foo.some_call(matching!()).returns("mocked"));
assert_eq!("mocked", mocked.foo());
sourcepub fn new_partial(setup: impl Clause) -> Self
pub fn new_partial(setup: impl Clause) -> Self
Construct a unimock instance using partial mocking.
In a partially mocked environment, every clause acts as an override over the default behaviour, which is to hit “real world” code.
Trait methods which support the unmock
feature get this behaviour automatically in a partial mock, unless explicitly overridden in the passed Clause.
Methods that cannot be unmocked still need to be explicitly mocked with a clause.
Example
#[unimock(api=TraitMock, unmock_with=[real_foo])]
trait Trait {
fn foo(&self) -> &'static str;
}
fn real_foo(_: &impl std::any::Any) -> &'static str {
"real thing"
}
// A partial mock with no overrides:
assert_eq!("real thing", Unimock::new_partial(()).foo());
// A partial mock that overrides the behaviour of `Trait::foo`:
let clause = TraitMock::foo.next_call(matching!()).returns("mocked");
assert_eq!("mocked", Unimock::new_partial(clause).foo());