[−][src]Macro mockall::expectation
Generate Expectation and Expectations types for a single method.
This macro can mock most method types, whether they take self
, &self
, or
&mut self
references, 'static
arguments or reference arguments, and
'static
or reference return types.
Reference arguments will be given independent anonymous lifetimes. This is the usual behavior for methods that don't specify explicit lifetimes.
Methods that return references will use self
's lifetime for the lifetime
of the returned value.
Generic methods are allowed, as long as the generic parameters are 'static
(but concrete types don't need to be). Don't repeat the generic bounds in
the macro invocation.
Arguments
vis
: Visibility qualifier relative toexpectation!
's private module. Private visibility is not really useful.module
: Name of the module to creategenerics
: Comma-delimited sequence of generic parameters, sans bounds.o
: Owned version of the output type. Must be a'static
. The real output type will be a reference to this one. Returning references, both mutable and immutable, is allowed, but note that the&
or&mut
is technically not part of the macro argument.argty
: Comma-delimited sequence of arguments types for the method being mocked.matchcall
: Comma-delimited sequence of expressions that produce values of typematchty
from values of typeargty
.args
: comma-delimited sequence of argument names for each argument. Ideally this wouldn't need to be specified, but it is due to Rust's macro hygiene rules.altargs
: Comma-delimited sequence of identifiers of the same length asargs
, but distinct.matchty
: comma-delimited sequence of types for each match argument. Must all be'static
.
TODO: document generated methods
Examples
Mock a method with a 'static
return type like
foo(&self, x: u32, y: &i16) -> u32
expectation! { pub fn foo<>(&self, x: u32, y: &i16) -> u32 { let (px: &u32, py: &i16) = (&x, y); } }
Mock a generic method with a 'static
return type like
foo<D: Clone>(d: D, x: &u32) -> bool
expectation! { pub fn foo<D>(&self, d: D, x: &u32) -> bool { let (pd: &D, px: &u32) = (&d, x); } }
Mock a method returning a reference like
foo(&self, x: u32, y: &i16) -> &u32
expectation!{ pub fn foo<>(&self, i0: u32, i1: &i16) -> &u32 { let (p0: &u32, p1: &i16) = (&i0, i1); } }
Mock a method returning a mutable reference like
foo(&mut self, x: u32, y: &i16) -> &mut u32
expectation!{ pub fn foo<>(&mut self, i0: u32, i1: &i16) -> &mut u32 { let (p0: &u32, p1: &i16) = (&i0, i1); } }