-
Notifications
You must be signed in to change notification settings - Fork 69
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
Mock trait with methods that return iterators #547
Comments
This is a complicated trait. Does it work if you define that method like this? |
with this modification, I have the following error:
|
Did you try following that suggestion and adding the bound? |
I could make it work like this, but I not sure if it is exactly what the suggestion was saying: #[automock(type T=String;)]
trait Foo {
type T: 'static;
fn iter(&self) -> impl Iterator<Item = usize>;
fn iter_mut(&mut self) -> impl Iterator<Item = &'static mut <Self as Foo>::T>;
} However, it seems odd to me to require a static lifetime in this trait. Am I missing something? |
That's actually not as odd as you might think. In order to store the expectation, Mockall requires that the lifetime of return values be either the same as the object itself, or else |
Thanks a lot! I realize that I need a deeper understanding of lifetimes. I am quite new to the concept. |
Could you consider clarifying what "the same as the object itself" means in the documentation? I stumbled upon the issue a while ago, and clearer documentation would have been helpful. Thanks! |
When mocking a function that returns a reference, Mockall stores that referent within the mock object. For example: #[automock]
pub trait Foo {
fn foo(&self) -> &i32;
}
#[test]
fn t() {
let mut mock = MockFoo::new();
mock.expect_foo()
.return_ref(42i32);
} As you can see, we store a real |
Hello,
I am trying to mock a trait with two iterator methods.
iter
anditer_mut
, each of which returns an iterator to references and mutable references of a parametric type.The code below does not compile, but the problem comes from the
iter_mut
method. The other one works just fine.I could not find an example in the tests that resemble what I am trying to do.
the compiler raises the following errors:
The text was updated successfully, but these errors were encountered: