Coder Social home page Coder Social logo

Comments (6)

TedSander avatar TedSander commented on July 2, 2024

I definitely think there is a couple of problems here:
a) Documentation thenReturn is used 18 times in the README thenAnswer is used once. There is no documentation on how to use either.
b) Naming the two aren't really clear in the name alone what the difference is, and I think return is the more natural name.
c) I do think the dart language makes thenReturn easier to use then thenAnswer, but people don't understand the the object is getting created right away.

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.

from mockito.

alanrussian avatar alanrussian commented on July 2, 2024

+1. A lot of people on my team have ran into this issue.

from mockito.

matanlurey avatar matanlurey commented on July 2, 2024

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:

What if we did an assert in thenReturn and threw if the type was a Future or Stream?

from mockito.

alanrussian avatar alanrussian commented on July 2, 2024

I will proceed with the assertion idea. I'll try to work on this soon and send a pull request.

from mockito.

dskloetg avatar dskloetg commented on July 2, 2024

The fix in commit #88 does not guarantee that the future is created inside the callback, nor does it even suggest that it should.
If people simply follow the instructions in the assertion message, it is likely not to make any difference.

"Invalid argument(s): thenReturn should not be used to return a Future. Instead, use thenAnswer((_) => future)."

Maybe the assertion message should explain why?

from mockito.

mattalbr avatar mattalbr commented on July 2, 2024

+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 thenAnswer((_) async => future) worked 100% of the time, possibly because it guarantees the future is created inside the callback? If that's a viable solution and not just dumb luck, it might be nice to change the assertion message to suggest making the lambda async.

from mockito.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.