Skip to content
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

Closed
RomanKapshuk opened this issue Sep 6, 2019 · 6 comments
Closed

How to mock second call? #221

RomanKapshuk opened this issue Sep 6, 2019 · 6 comments

Comments

@RomanKapshuk
Copy link

RomanKapshuk commented Sep 6, 2019

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?

  final api = Api();


  Info _getRemoteInfo() async {

    try {
      final result = await api.getInfo(); // should throw Exception
      return result;
    } catch (ex) {
     // Do some stuff to fix the api issue;
      final result = await api.getInfo(); // should return Info object
      return result;  
    }

  }
@srawlins
Copy link
Member

srawlins commented Sep 6, 2019

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:

```dart
final api = Api();
...
```

When you stub a method call like when(foo.bar()).thenReturn(7), then 7 will be returned every time bar() is called, including the first, second, ...

@RomanKapshuk
Copy link
Author

RomanKapshuk commented Sep 6, 2019

@srawlins Thanks.
Ok, but what about to make the ability to add a sequence of expected values?
Something like

when(foo.bar()).thenReturn([1, 2, 3]);
when(foo.bar()).thenAnswer([() async => 1, () async => 2, () async => 3]);
etc..

So on every new call of bar() method – it will returns next value in the list?

final mockedFoo = MockedFoo();

test('', () {
when(mockedFoo.bar()).thenReturn([1, 2, 3]);

final firstResult = mockedFoo.bar(); // 1
final secondResult = mockedFoo.bar(); // 2
final thirdResult = mockedFoo.bar(); // 3

});

@srawlins
Copy link
Member

srawlins commented Sep 6, 2019

Mockito doesn't have a mechanism for that, but I could see us adding something like thenReturnInOrder and thenAnswerInOrder.

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

@RomanKapshuk
Copy link
Author

@srawlins Sure, it would be awesome! Thanks!

@KohlsAdrian
Copy link

Mockito doesn't have a mechanism for that, but I could see us adding something like thenReturnInOrder and thenAnswerInOrder.

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

That's a godlike solution

@MJablonskiCodespot
Copy link

MJablonskiCodespot commented Apr 3, 2024

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),
    ]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants