-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow mocking some methods with generic non-static arguments #408
Conversation
6a29d57
to
0981e83
Compare
Add a #[mockall::concretize] attribute. When set on a function or method, its generic expectations will be turned into trait objects. But it only works for function arguments that are pure generic types or a few basic combinations: * T * &T * &mut T * &[T] Issue #217
0981e83
to
f81342d
Compare
This seems great and I'd love to use it, but unless I'm doing something very wrong, it doesn't seem to work if Specifically, the problem is that I'm guessing this is something to do with the following line in the docs, suggesting that the attribute is more of a textual marker than taking effect directly:
Is this fixable? |
Hm, you must be doing something wrong, because I use |
Let me provide an example, and hopefully you can tell me what I'm doing wrong. The following is based on the example in the docstring: use std::path::Path;
#[cfg(test)]
use mockall::{automock, concretize};
#[cfg_attr(test, automock)]
trait Foo {
#[concretize]
fn foo<P: AsRef<Path>>(&self, p: P);
}
fn main() {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn some_test() {
let mut mock = MockFoo::new();
mock.expect_foo()
.withf(|p| p.as_ref() == Path::new("/tmp"))
.return_const(());
mock.foo(Path::new("/tmp"));
}
} This works fine in test mode, but
The last two errors seem to just be the effects of How do I get |
Yep, it's a bug. I never noticed because I've only used concretize with |
Add a #[mockall::concretize] attribute. When set on a function or method, its generic expectations will be turned into trait objects.
But it only works for function arguments that are pure generic types or a few basic combinations:
Issue #217