-
-
Notifications
You must be signed in to change notification settings - Fork 794
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
Adding Callback
to a mock breaks async tests
#702
Comments
Hi @marcin-chwedczuk-meow - what you are seeing here has nothing to do with (See also #673 (comment) for a more detailed explanation of how Moq determines methods' return values.) |
@stakx I understand your point, the rule for determining return values is simple and easy to follow. On the other hand if you are on .NET Core using async/await is a must (e.g. HttpClient provides only async API), and this Moq behavior is just uncomfortable when testing async method. I created this issue because I and my coworker wasted about 30 min of our time trying to figure out why our test is failing with seemingly unrelated exception. I believe that async methods are a very special case that needs a special handling... |
@marcin-chwedczuk-meow - What exactly are you suggesting?
If this is your full suggestion, please be aware that implementing this change has the potential of breaking other users' code. It would be far easier for you to simply add a (Note, I do agree with you in principle. Moq's current way of determining a method's return value isn't exactly logical. I'd like to see this changed, but it's probably not worth the breaking change since there are easy workarounds available.) |
Adding |
@marcin-chwedczuk-meow - I totally understand where you're coming from, and I agree there's a trap waiting here for anyone not overly familiar with Moq. However, I don't buy the "juniors or less experienced programmers" argument. If a programmer does not know how to analyze and resolve a That being said, I am not sure how to best "fix" this problem. It'd be nice to simply make Moq's rules for determining return values less arcane, but that'd be a breaking change. Documenting the current rules would likely just make things more confusing, not less. So if we take the "better documentation" route, we should perhaps just state somewhere (possibly at the end of the Methods section in the Quickstart) that every setup for a non- Please feel free to improve the Quickstart page in the Wiki. |
Possibly related: #327. |
After looking at #794 I believe we could resolve this issue here by adding a mock.Setup(m => m.DoSomethingAsync()).CallbackAsync(async () => { ... }); (I know, lots of Under the hood, it might be implemented like a simple |
Another solution is to use Moq.SetupAsync which provide an extension method named SetupAsync on moq and will provide you all the you need to easily mock async methods respecting the state machine generated by dotnet Once you add that lib to your project
you can use the SetupAsync method and simply write
and get many more easy to use methods adapted to async scnearios |
I have the following csproj:
and the following test class:
The test fails because
DoMoreStuffAsync
returnsnull
which breaksasync
state machine generated by C# compiler. Uncommenting line that I calledline X
solves the problem.In my opinion
async
methods should be treated specially by mock, and generally they should never returnnull
(as this is certainly a mistake).Expected behavior: When I define a callback on an async method it should by default return
Task.CompletedTask
to the caller so that it will not break async code.The text was updated successfully, but these errors were encountered: