-
Notifications
You must be signed in to change notification settings - Fork 63
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
Use async function in something like returning #252
Comments
Mockall has special handling for Or do you want to return a future that isn't ready yet? An async method is just fancy syntactical sugar for a method that returns a future. So if you dispense with |
I'm aware of the async_trait crate and I've been using that with mockall for some time. But when handling the invocation to a mocked async function, I want to be able to use a lambda. The API has a Is that clearer? |
Can you just do this? .returning(move || async move {
whatever().await
}) |
I don't actually need the moves because the thing I want to await on is a function on one of the parameters, but either way, it doesn't work. It complains that it was expecting the return type of the async function - in this case () - but instead it found a future.
|
That suggests that your original method is defined as an .returning(move |...| async move {
something.await;
()
}.now_or_never().unwrap()
) But if you really want to return a future that isn't ready yet, then you can't use an mock!{
Foo {
// async fn foo(...) -> u32; // If the real method looks like this, then mock it like:
fn foo(...) -> impl Future<Output=u32>;
}
} |
Yes - it is an async function and I do want to return a future that isn't necessarily ready yet. Is this something mockall will support at some point in the future - perhaps when async fn is a first class feature? |
Potentially. It's something I use too on my own project. For me, the "normal function that returns a Future" method works fine. But I would have to figure out a clean syntax for it, and of course do all the work. |
It sounds like I've answered your question. And as a feature request, your request is the same as #189 . So I'm closing this issue as a duplicate. |
I can't see anything in the API that would allow me to execute some async function when handling a method invocation.
Perhaps something like:
returning_async(|o| o.do_something_async.await)
The text was updated successfully, but these errors were encountered: