Struct unimock::Unimock

source ·
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§

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());

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());

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.