pub trait MockFn: Sized + 'static {
type Inputs<'i>;
type OutputKind: Kind;
type AnswerFn: ?Sized + Send + Sync;
// Required method
fn info() -> MockFnInfo;
// Provided methods
fn debug_inputs(inputs: &Self::Inputs<'_>) -> Box<[Option<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§
sourcetype Inputs<'i>
type Inputs<'i>
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 beT
. - For a function with N parameters, the type should be the tuple
(T1, T2, ..)
.
sourcetype OutputKind: Kind
type OutputKind: Kind
A type that describes how the mocked function responds.
The Kind 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:
- The user specifies it upfront as part of a Clause.
- The conversion of this value into the mocked function’s final output value.
Required Methods§
sourcefn info() -> MockFnInfo
fn info() -> MockFnInfo
Static information about the mocked method
Provided Methods§
sourcefn debug_inputs(inputs: &Self::Inputs<'_>) -> Box<[Option<String>]>
fn debug_inputs(inputs: &Self::Inputs<'_>) -> Box<[Option<String>]>
Compute some debug representation of the inputs.
sourcefn stub<E>(self, each_fn: E) -> Each<Self>
fn stub<E>(self, each_fn: E) -> Each<Self>
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.
sourcefn some_call(
self,
matching_fn: &dyn Fn(&mut Matching<Self>)
) -> DefineResponse<'static, Self, InAnyOrder>
fn some_call( self, matching_fn: &dyn Fn(&mut Matching<Self>) ) -> DefineResponse<'static, Self, InAnyOrder>
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.
sourcefn each_call(
self,
matching_fn: &dyn Fn(&mut Matching<Self>)
) -> DefineMultipleResponses<'static, Self, InAnyOrder>
fn each_call( self, matching_fn: &dyn Fn(&mut Matching<Self>) ) -> DefineMultipleResponses<'static, Self, InAnyOrder>
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.
sourcefn next_call(
self,
matching_fn: &dyn Fn(&mut Matching<Self>)
) -> DefineResponse<'static, Self, InOrder>
fn next_call( self, matching_fn: &dyn Fn(&mut Matching<Self>) ) -> DefineResponse<'static, Self, InOrder>
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.
Object Safety§
Implementors§
source§impl MockFn for unimock::mock::core::fmt::DebugMock::fmt
Available on crate feature mock-core
only.
impl MockFn for unimock::mock::core::fmt::DebugMock::fmt
mock-core
only.source§impl MockFn for unimock::mock::core::fmt::DisplayMock::fmt
Available on crate feature mock-core
only.
impl MockFn for unimock::mock::core::fmt::DisplayMock::fmt
mock-core
only.source§impl MockFn for unimock::mock::core::hash::HasherMock::write
Available on crate feature mock-core
only.
impl MockFn for unimock::mock::core::hash::HasherMock::write
mock-core
only.source§impl MockFn for write_i128
Available on crate feature mock-core
only.
impl MockFn for write_i128
mock-core
only.source§impl MockFn for write_isize
Available on crate feature mock-core
only.
impl MockFn for write_isize
mock-core
only.source§impl MockFn for write_u128
Available on crate feature mock-core
only.
impl MockFn for write_u128
mock-core
only.source§impl MockFn for write_usize
Available on crate feature mock-core
only.
impl MockFn for write_usize
mock-core
only.source§impl MockFn for unimock::mock::embedded_hal_1::digital::ErrorMock::kind
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for unimock::mock::embedded_hal_1::digital::ErrorMock::kind
mock-embedded-hal-1
only.source§impl MockFn for is_set_high
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for is_set_high
mock-embedded-hal-1
only.source§impl MockFn for is_set_low
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for is_set_low
mock-embedded-hal-1
only.source§impl MockFn for unimock::mock::embedded_hal_1::i2c::ErrorMock::kind
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for unimock::mock::embedded_hal_1::i2c::ErrorMock::kind
mock-embedded-hal-1
only.source§impl MockFn for unimock::mock::embedded_hal_1::pwm::ErrorMock::kind
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for unimock::mock::embedded_hal_1::pwm::ErrorMock::kind
mock-embedded-hal-1
only.source§impl MockFn for max_duty_cycle
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for max_duty_cycle
mock-embedded-hal-1
only.source§impl MockFn for set_duty_cycle
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for set_duty_cycle
mock-embedded-hal-1
only.source§impl MockFn for set_duty_cycle_fraction
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for set_duty_cycle_fraction
mock-embedded-hal-1
only.source§impl MockFn for set_duty_cycle_fully_off
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for set_duty_cycle_fully_off
mock-embedded-hal-1
only.source§impl MockFn for set_duty_cycle_fully_on
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for set_duty_cycle_fully_on
mock-embedded-hal-1
only.source§impl MockFn for set_duty_cycle_percent
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for set_duty_cycle_percent
mock-embedded-hal-1
only.source§impl MockFn for unimock::mock::embedded_hal_1::spi::ErrorMock::kind
Available on crate feature mock-embedded-hal-1
only.
impl MockFn for unimock::mock::embedded_hal_1::spi::ErrorMock::kind
mock-embedded-hal-1
only.source§impl MockFn for unimock::mock::futures_0_3::io::AsyncBufReadMock::consume
Available on crate feature mock-futures-io-0-3
only.
impl MockFn for unimock::mock::futures_0_3::io::AsyncBufReadMock::consume
mock-futures-io-0-3
only.source§impl MockFn for unimock::mock::futures_0_3::io::AsyncBufReadMock::poll_fill_buf
Available on crate feature mock-futures-io-0-3
only.
impl MockFn for unimock::mock::futures_0_3::io::AsyncBufReadMock::poll_fill_buf
mock-futures-io-0-3
only.source§impl MockFn for unimock::mock::futures_0_3::io::AsyncReadMock::poll_read
Available on crate feature mock-futures-io-0-3
only.
impl MockFn for unimock::mock::futures_0_3::io::AsyncReadMock::poll_read
mock-futures-io-0-3
only.source§impl MockFn for poll_read_vectored
Available on crate feature mock-futures-io-0-3
only.
impl MockFn for poll_read_vectored
mock-futures-io-0-3
only.source§impl MockFn for poll_close
Available on crate feature mock-futures-io-0-3
only.
impl MockFn for poll_close
mock-futures-io-0-3
only.source§impl MockFn for unimock::mock::futures_0_3::io::AsyncWriteMock::poll_flush
Available on crate feature mock-futures-io-0-3
only.
impl MockFn for unimock::mock::futures_0_3::io::AsyncWriteMock::poll_flush
mock-futures-io-0-3
only.source§impl MockFn for unimock::mock::futures_0_3::io::AsyncWriteMock::poll_write
Available on crate feature mock-futures-io-0-3
only.
impl MockFn for unimock::mock::futures_0_3::io::AsyncWriteMock::poll_write
mock-futures-io-0-3
only.source§impl MockFn for unimock::mock::futures_0_3::io::AsyncWriteMock::poll_write_vectored
Available on crate feature mock-futures-io-0-3
only.
impl MockFn for unimock::mock::futures_0_3::io::AsyncWriteMock::poll_write_vectored
mock-futures-io-0-3
only.source§impl MockFn for unimock::mock::std::io::BufReadMock::consume
Available on crate feature mock-std
only.
impl MockFn for unimock::mock::std::io::BufReadMock::consume
mock-std
only.source§impl MockFn for read_until
Available on crate feature mock-std
only.
impl MockFn for read_until
mock-std
only.source§impl MockFn for read_exact
Available on crate feature mock-std
only.
impl MockFn for read_exact
mock-std
only.source§impl MockFn for read_to_end
Available on crate feature mock-std
only.
impl MockFn for read_to_end
mock-std
only.source§impl MockFn for read_to_string
Available on crate feature mock-std
only.
impl MockFn for read_to_string
mock-std
only.source§impl MockFn for read_vectored
Available on crate feature mock-std
only.
impl MockFn for read_vectored
mock-std
only.source§impl MockFn for stream_position
Available on crate feature mock-std
only.
impl MockFn for stream_position
mock-std
only.source§impl MockFn for unimock::mock::std::io::WriteMock::write
Available on crate feature mock-std
only.
impl MockFn for unimock::mock::std::io::WriteMock::write
mock-std
only.source§impl MockFn for write_vectored
Available on crate feature mock-std
only.
impl MockFn for write_vectored
mock-std
only.source§impl MockFn for unimock::mock::tokio_1::io::AsyncBufReadMock::consume
Available on crate feature mock-tokio-1
only.
impl MockFn for unimock::mock::tokio_1::io::AsyncBufReadMock::consume
mock-tokio-1
only.source§impl MockFn for unimock::mock::tokio_1::io::AsyncBufReadMock::poll_fill_buf
Available on crate feature mock-tokio-1
only.
impl MockFn for unimock::mock::tokio_1::io::AsyncBufReadMock::poll_fill_buf
mock-tokio-1
only.source§impl MockFn for unimock::mock::tokio_1::io::AsyncReadMock::poll_read
Available on crate feature mock-tokio-1
only.
impl MockFn for unimock::mock::tokio_1::io::AsyncReadMock::poll_read
mock-tokio-1
only.source§impl MockFn for poll_complete
Available on crate feature mock-tokio-1
only.
impl MockFn for poll_complete
mock-tokio-1
only.source§impl MockFn for start_seek
Available on crate feature mock-tokio-1
only.
impl MockFn for start_seek
mock-tokio-1
only.source§impl MockFn for is_write_vectored
Available on crate feature mock-tokio-1
only.
impl MockFn for is_write_vectored
mock-tokio-1
only.source§impl MockFn for unimock::mock::tokio_1::io::AsyncWriteMock::poll_flush
Available on crate feature mock-tokio-1
only.
impl MockFn for unimock::mock::tokio_1::io::AsyncWriteMock::poll_flush
mock-tokio-1
only.source§impl MockFn for poll_shutdown
Available on crate feature mock-tokio-1
only.
impl MockFn for poll_shutdown
mock-tokio-1
only.source§impl MockFn for unimock::mock::tokio_1::io::AsyncWriteMock::poll_write
Available on crate feature mock-tokio-1
only.
impl MockFn for unimock::mock::tokio_1::io::AsyncWriteMock::poll_write
mock-tokio-1
only.source§impl MockFn for unimock::mock::tokio_1::io::AsyncWriteMock::poll_write_vectored
Available on crate feature mock-tokio-1
only.
impl MockFn for unimock::mock::tokio_1::io::AsyncWriteMock::poll_write_vectored
mock-tokio-1
only.