Attribute Macro mockall::concretize
#[concretize]
Expand description
Decorates a method or function to tell Mockall to treat its generic arguments as trait objects when creating expectations.
This allows users to use non-'static
generic parameters, which otherwise
can’t be mocked. The downsides of using this attribute are:
- Mockall can’t tell if a parameter isn’t
'static
, so you must annotate such methods with the#[mockall::concretize]
attribute. - Generic methods will share expectations for all argument types. That is,
you won’t be able to do
my_mock.expect_foo::<i32>(...)
. - It can’t be used on methods with a closure argument (though this may be fixable).
- Concretized methods’ expectations may only be matched with
.withf
or.withf_st
, not.with
. - It only works for parameters that can be turned into a trait object. (may be fixable).
- Mockall needs to know how to turn the function argument into a trait
object. Given a generic parameter
T
, currently supported patterns are:T
&T
&mut T
&[T]
§Examples
#[automock]
trait Foo {
#[mockall::concretize]
fn foo<P: AsRef<Path>>(&self, p: P);
}
let mut mock = MockFoo::new();
mock.expect_foo()
.withf(|p| p.as_ref() == Path::new("/tmp"))
.return_const(());
mock.foo(Path::new("/tmp"));
NB: This attribute must be imported with its canonical name. It won’t work otherwise!
ⓘ
use mockall::concretize as something_else;
#[mockall::automock]
trait Foo {
#[something_else]
fn foo<T>(&self, t: T);
}