-
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
Dart Mockito when first, second, ... n calls, return different value #260
Comments
There is not a built-in way to do this. See my response here. We could add an API like |
I've also faced the same problem where I needed to return different values to the same call - in the same test. @srawlins your response did the trick for me, but I think it would be way more readable if there is some API like the one you suggested. |
An api like
See the example in the docs here: https://javadoc.io/static/org.mockito/mockito-core/4.2.0/org/mockito/Mockito.html#10 |
@danielgomezrico do you have an API in mind? |
Yes, I checked with this small closure, and it works: extension MultipleExpectations<T> on PostExpectation<T> {
void thenAnswerInOrder(List<T> bodies) {
final answers = Queue.of(bodies);
thenAnswer((_) => answers.removeFirst());
}
} And then use it like: when(mock).thenAnwserInOrder(['First call', 'second call']); I could open a PR with that, maybe we could add another one that accepts lambdas and return the execution to allow throwing exceptions? something like: when(mock).thenAnwserInOrder([
() => 'First call',
() => 'second call',
() => throw 'BOOOM']
); |
Other than that, I like the API. We'll have to make sure the implementation works with other |
And why return works better? I thought it would fit better because in the internals it uses |
I have one question... what should it return, then the function was called more times than the number of times it was declared? it should throw an exception or what? |
They do, but I think it is more important for the name to follow the pattern of other interfaces ( |
I think it should follow the current behavior, which is to use a |
Is there any news about this topic? Is there any expectation of I think that many of us that are coming to Dart from Java/Kotlin will have this need. @srawlins your workaround works fine though. |
I apologize for taking too long, I will start working on it this week 🤓 |
Ok, so I reviewed the PR without looking at this bug. So I overlooked the requirement to return to the default response once we are out of provided responses. Returning back to default seems doable, but I'm undecided if it would actually follow the previous behavior, since all of |
@yanok can you reword your comment? I don't understand what do you mean. |
I mean I've read the previous discussion:
and realized we didn't do what @srawlins requested there: currently if we run out of provided responses we don't return to using @srawlins Sam, do you still think we should try to get back to |
I this this behavior is fine. I don't think I strongly preferred |
ok, thanks Sam, let's close this as implemented then. |
@srawlins Im trying to do this with Futures. My method im mocking returns a future and in the first invocation I am trying to throw an error but then in the second invocation I am returning a valid value. I seem to not be able to have the framework work with this with some errors being thrown. the code being tested is as following
And in my test i have
When the test runs, the first invocation is throws and is handled correctly, on the second invocation the code passes the method and execution continues normally but but I get the following output in the logs and the test fails
I know support for thenAnswerInOrder has not been implemented but is there some incompatibility to make it work thenAnswer? |
It seems if I rework the code to not use arrays and just an if/else block it works, so Im not sure if Im doing something wrong by returning futures in that way or there is a bug. |
In Java Mockito, you can Mock a different returned value for each call like this:
Or like this:
Source: https://stackoverflow.com/questions/11785498/simulate-first-call-fails-second-call-succeeds
I wonder how to achieve the same in Dart Mockito. I could not find a documentation for that
The text was updated successfully, but these errors were encountered: