-
Notifications
You must be signed in to change notification settings - Fork 160
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
Push users to .thenAnswer(<Future|Stream>)? #79
Comments
I definitely think there is a couple of problems here: A lot of times using thenReturn is safe since when an object is created isn't that important. Async types are a very painful exception. We should definitely fix a, and b. What if we did an assert in thenReturn and threw if the type was a Future or Stream? If we needed to we could have a boolean optional variable of 'acceptAsyncTypes' of course I'm not sure if there is ever a case we should allow them. |
+1. A lot of people on my team have ran into this issue. |
I'm definitely motivated to fix this @alanrussian @TedSander. That being said, I'm low on cycles. I'd be happy to review code/strategies from @alanrussian and his team on moving forward. I think this works for me:
|
I will proceed with the assertion idea. I'll try to work on this soon and send a pull request. |
Also, documented this behavior in the README. This fixes dart-lang#79.
Mockito now prohibits calling thenReturn with Futures and Streams. dart-lang/mockito#79
Mockito now prohibits calling thenReturn with Futures and Streams. dart-lang/mockito#79
Mockito now prohibits calling thenReturn with Futures and Streams. dart-lang/mockito#79
* Change async stubbing to use thenAnswer. Mockito now prohibits calling thenReturn with Futures and Streams. dart-lang/mockito#79 * Update all Mockito deps to 3.0.0. * Revert "Update all Mockito deps to 3.0.0." This reverts commit e8ab9d3. I did not correctly update the mockito dep, and there's no easy way to update to 3.0 alpha right now. * Change thenAnswer((_) => to thenAnswer((invocation) => * Add Invocation type to thenAnswer lambdas
* Change async stubbing to use thenAnswer. Mockito now prohibits calling thenReturn with Futures and Streams. dart-lang/mockito#79 * Update all Mockito deps to 3.0.0. * Revert "Update all Mockito deps to 3.0.0." This reverts commit e8ab9d3. I did not correctly update the mockito dep, and there's no easy way to update to 3.0 alpha right now. * Change thenAnswer((_) => to thenAnswer((invocation) => * Add Invocation type to thenAnswer lambdas
The fix in commit #88 does not guarantee that the future is created inside the callback, nor does it even suggest that it should. "Invalid argument(s): Maybe the assertion message should explain why? |
+1 lost 2-3 hours debugging yesterday because I followed the assertion message verbatim, definitely want to echo how nearly undebuggable this makes the test (if I ran the test once, it worked fine. Twice, any code after awaiting that future would never run). I'm not a Dart expert, so I don't really know why, but for me it seemed doing |
This is what I see a lot:
But it can break in basically un-debuggable ways, especially around zones. Normally a
Future
orStream
won't be created until an invocation is made, but with the.thenReturn
pattern, it has been created in the test setup.The "correct" behavior here would be to use
.thenAnswer
:I don't know if this a documentation, API, or just UX issue, or there is even a real issue - maybe mockito alone is mysterious so nobody assumes code to work great 100% of the time. We could make the API more verbose, i.e.:
But I don't know if that would help unless we threw on an invalid type at runtime.
The text was updated successfully, but these errors were encountered: