-
-
Notifications
You must be signed in to change notification settings - Fork 799
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
Feature request: CallbackAsync #737
Comments
This reminds me of two issues over at the repo for DynamicProxy (Castle.Core) about async interception. I suspect that even if we tried to implement what you ask for, we'd run into a roadblock with DynamicProxy and thread safety issues. But perhaps we don't even have to get that far... I suspect there might be a problem with the feature request per se: If you are setting up a method that returns a If you are set on setting up a method that does not return a |
Yeah, I was thinking this when I was writing the request. It sounds impossible. I asked on stack overflow and someone had the great idea that I should just assign the task parameter to a variable in an outside scope and await it there. I'm just so used to writing the callback like: param => param.thing.should().Be(correct value) that it just didn't occur to me to assign it to a variable outside the lambda. Feel free to close the request if it's impractical or impossible as I have a perfectly good workaround for now. At least if anyone else is being as dense as me (unlikely I know) and they're looking for a similar feature hopefully they'll come across this and can make use of this technique. |
Expanded my answer above a little. Let's leave your issue open for tiny little while longer, in case someone has any another idea or suggestion. |
Actually, I've just thought about it a teensy bit longer. The only way I see it working is if you did something like:
|
@jamietwells - Agreed that something like it is generally feasible. It should be possible to implement such a I'm however cautious about having something like this added to Moq. It may not be a frequent-enough scenario to warrant addition to the core library, and it may actually hinder an understanding of how async works; i.e. how the task inside the callback gets captured and how it relates to the actual method being called. Some people may end up thinking that a method called |
Closing for the reasons already given above. Better IMO to let users capture the task inside the callback (e.g. using a capturing matcher, i.e. |
If you're mocking an implementation and a class has method you want to test it might have a parameter that is a
Task<something>
if this is the case, you might want to ensure the something is correct so you might do a callback:Unfortunately, the Callback signature is Action<Task> not Func<Task, Task> so the callback is not awaited and the exception is lost (in this case it is of course the NRE from awaiting null).
(This came about because I was trying to verify the Content of an HttpRequestMessage was the expected content, but of course you must do request.Content.ReadAsStringAsync() to get the passed string, which I couldn't do!)
I know just thinking about it it seems impossible to implement because where would the await go? Inside wherever the callback is called but then who would await that task? At the end of the day the test is the starting point and there's no guarantee the test is async.
Anyway, I just thought I'd ask and get an opinion on if this feature seems possible, and can I also say how much I love this package! :)
The text was updated successfully, but these errors were encountered: