Struct unimock::Unimock

source ·
pub struct Unimock { /* private fields */ }
Expand description

A type whose purpose is to provide mocked behaviour for the traits that it implements.

All traits implemented by Unimock can be considered mock implementations, except marker traits, Clone and Drop.

The mock configuration is specified up front, as a constructor argument in the form of a simple or compound Clause. After instantiation, the unimock configuration is immutable.

Unimock implements Send and Sync, and is therefore thread safe.

Unimock is meant to be used as a testing utility. Using it for other purposes is not recommended.

Clone semantics

Calling clone on unimock creates a derived object which shares internal state with the original.

Unimock runs post-test verifications automatically upon drop (it is a RAII utility). Interaction verifications should be performed only after all expected interactions have completed. Because of this, only the original instance will run any verifications upon being dropped, and when dropping the original instance, it is an error if there are derived objects still alive. In a typical testing context, this scenario usually means that a spawned thread (that owns a derived Unimock) has escaped the test. Unimock will panic with an appropriate message if this is detected.

This detection is usually reliable. Unimock will also induce a panic if the original instance gets dropped in a thread that does not equal the creator thread. Therefore, Unimock should always be cloned before sending off to another thread.

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.