Trait unimock::MockFn

source ·
pub trait MockFn: Sized + 'static {
    type Inputs<'i>;
    type Response: Respond;
    type Output<'u>: Output<'u, Self::Response>;

    const NAME: &'static str;

    fn debug_inputs(inputs: &Self::Inputs<'_>) -> String;

    fn stub<E>(self, each_fn: E) -> Each<Self>
    where
        E: FnOnce(&mut Each<Self>)
, { ... } fn some_call(
        self,
        matching_fn: &dyn Fn(&mut Matching<Self>)
    ) -> DefineResponse<'static, Self, InAnyOrder> { ... } fn each_call(
        self,
        matching_fn: &dyn Fn(&mut Matching<Self>)
    ) -> DefineMultipleResponses<'static, Self, InAnyOrder> { ... } fn next_call(
        self,
        matching_fn: &dyn Fn(&mut Matching<Self>)
    ) -> DefineResponse<'static, Self, InOrder> { ... } }
Expand description

The main trait used for unimock configuration.

MockFn describes functional APIs that may be called via dispatch, a.k.a. Inversion of Control. Virtuality should be regarded as as test-time virtuality: A virtual function is either the real deal OR it is mocked.

In Rust, the most convenient way to perform a virtualized/dispatched function call is to call a trait method.

MockFn only provides metadata about an API, it is not directly callable.

As this is a trait itself, it needs to be implemented to be useful. Methods are not types, so we cannot implement MockFn for those. But a surrogate type can be introduced:

trait MockMe {
    fn method(&self);
}

// The method can be referred to via the following empty surrogate struct:
mod MockMeMock {
    pub struct method;
}

/* impl MockFn for MockMeMock::method ... */

Required Associated Types§

The inputs to a mockable function.

  • For a function with no parameters, the type should be the empty tuple ().
  • For a function with 1 parameter T, the type should be T.
  • For a function with N parameters, the type should be the tuple (T1, T2, ..).

A type that describes how the mocked function responds.

The Respond trait describes a type used internally to store a response value.

The response value is Unimock’s internal representation of the function’s return value between two points it time:

  1. The user specifies it upfront as part of a Clause.
  2. The conversion of this value into the mocked function’s final output value.

A type that describes the mocked function’s actual output type.

Required Associated Constants§

The name to use for runtime errors.

Required Methods§

Compute some debug representation of the inputs.

Provided Methods§

Create a stubbing clause by grouping calls.

A stub sets up call patterns on a single function, that can be matched in any order.

For exact order verification, reach for MockFn::next_call instead.

Define a stub-like call pattern directly on this MockFn.

This is a shorthand to avoid calling MockFn::stub if there is only one call pattern that needs to be specified on this MockFn.

As the method name suggests, this will not only configure mock behaviour, but also functions as an assertion that the call must happen.

This call pattern variant supports return values that do not implement Clone, therefore the call pattern can only be matched a single time.

Define a stub-like call pattern directly on this MockFn.

This is a shorthand to avoid calling MockFn::stub if there is only one call pattern that needs to be specified on this MockFn.

This variant is specialized for functions called multiple times.

Initiate a call pattern builder intended to be used as a Clause with exact order verification.

This differens from MockFn::stub, in that that a stub defines all call patterns without any specific required call order. This function takes only single input matcher, that MUST be matched in the order specified, relative to other next calls.

Implementors§