You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I ran into an extremely hard to diagnose bug. I wrote some tests and found that they were randomly failing with
Bad state: Cannot call `when` within a stub response
It was random because depending on if I randomized my tests sometimes the tests would pass or fail.
The reason for this was that on a prior test we had called when(() => someFunction()) but since the the function returns Future<void> I was under the assumption that the return value was unimportant and could be ignored. However, this meant that any subsequent when calls errored out, as _whenCall (inside mocktail.dart) was no longer null.
To Reproduce
Write any when call without a thenAnswer/thenReturn for a void/Future<void> function.
Write another when call for the same mocked class.
Expected behavior
Not actually sure. Should void/Future<void> be allowed by default without a then? This doesn't feel quite right. Or should there be a better error message like Missing then function for when, or just a hint on the current error message about making sure that your whens have following thens. I could also just be mistaken and there's a good reason for when() calls without a following then.
Logs
Bad state: Cannot call `when` within a stub response
package:mocktail/src/mocktail.dart 206:5 when
test/features/notifications/cubits/notification_preferences_cubit_test.dart 303:11 main.<fn>.<fn>.<fn>
package:bloc_test/src/bloc_test.dart 203:25 testBloc.<fn>
===== asynchronous gap ===========================
dart:async _Completer.completeError
package:bloc_test/src/bloc_test.dart 257:43 _runZonedGuarded.<fn>
===== asynchronous gap ===========================
dart:async _CustomZone.registerBinaryCallback
package:bloc_test/src/bloc_test.dart 254:5 _runZonedGuarded.<fn>
dart:async runZonedGuarded
package:bloc_test/src/bloc_test.dart 253:3 _runZonedGuarded
package:bloc_test/src/bloc_test.dart 200:11 testBloc
package:bloc_test/src/bloc_test.dart 156:13 blocTest.<fn>
The text was updated successfully, but these errors were encountered:
knyghtryda
changed the title
Should when() calls be allowed to not have a .then immediately after?
Should when() calls have a better error message for not having a following thenAnswer/thenReturn call?
Oct 12, 2023
Describe the bug
I ran into an extremely hard to diagnose bug. I wrote some tests and found that they were randomly failing with
It was random because depending on if I randomized my tests sometimes the tests would pass or fail.
The reason for this was that on a prior test we had called
when(() => someFunction())
but since the the function returnsFuture<void>
I was under the assumption that the return value was unimportant and could be ignored. However, this meant that any subsequentwhen
calls errored out, as_whenCall
(insidemocktail.dart
) was no longernull
.To Reproduce
when
call without athenAnswer/thenReturn
for avoid
/Future<void>
function.when
call for the same mocked class.Expected behavior
Not actually sure. Should
void
/Future<void>
be allowed by default without athen
? This doesn't feel quite right. Or should there be a better error message likeMissing then function for when
, or just a hint on the current error message about making sure that your whens have following thens. I could also just be mistaken and there's a good reason forwhen()
calls without a followingthen
.Logs
The text was updated successfully, but these errors were encountered: