-
Notifications
You must be signed in to change notification settings - Fork 164
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
How to mock second call? #221
Comments
In GitHub comments, make sure to put code examples in Markdown codeblocks. The easiest way to do this for your code above is to wrap your example in triple-backticks:
When you stub a method call like |
@srawlins Thanks.
So on every new call of bar() method – it will returns next value in the list?
|
Mockito doesn't have a mechanism for that, but I could see us adding something like For now, use a variable declared outside the "answer" response. var callCount = 0;
when(mockedFoo.bar()).thenAnswer((_) => [1, 2, 3][callCount++]); But make it more readable :P |
@srawlins Sure, it would be awesome! Thanks! |
That's a godlike solution |
Here's extension for it: import 'package:mockito/mockito.dart';
extension When<T> on PostExpectation<T> {
void thenAnswerInOrder(List<T> values) {
int callCount = 0;
thenAnswer((_) => values[callCount++]);
}
} And usage: final ChannelsDao channelsDao = MockChannelsDao();
final List<Channel> apiChannels = [MockChannel()];
final List<Channel> emptyChannelLis = [];
when(channelsDao.getAll()).thenAnswerInOrder([
Future.value(emptyChannelLis),
Future.value(apiChannels),
]); |
How to mock the second call of function?
E.g, I have the function, that calls API, and in case if it fails we are trying to make the same call and then expecting fine answer?
The text was updated successfully, but these errors were encountered: