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§

source§

impl Unimock

source

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

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

Trait Implementations§

source§

impl Clone for Unimock

source§

fn clone(&self) -> Unimock

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Drop for Unimock

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.