Coder Social home page Coder Social logo

Mock methods about mockito HOT 9 CLOSED

dart-lang avatar dart-lang commented on July 20, 2024
Mock methods

from mockito.

Comments (9)

kjawadDeveloper avatar kjawadDeveloper commented on July 20, 2024 4

@srawlins how we can do such testing?

from mockito.

TedSander avatar TedSander commented on July 20, 2024 2

You can use thenAnswer. It isn't quite as nice, but spy was not going to work without reflection.

final ft = new FooExample();
final mock = new MockFooExample();

  test('calculate', () {
    when(mock.myCalc()).thenReturn(10);
    when(mock.calculate()).thenAnswer(() => ft.calculate());
    expect(mock.myCalc(), 10);
    expect(mock.calculate(), 42);
  });

from mockito.

TedSander avatar TedSander commented on July 20, 2024 1

Sorry no. Spy used mirrors which can't be used in dart AOT environments such as flutter and soon web. #27

I don't believe this will be possible using our current implementation. It would require a complete rewrite of mockito using codegen which isn't happening anytime soon, and the migration may have other downsides such as slower build/dev cycle times.

For what it's worth in our corpus of code (millions of lines of code) we aren't missing it. It was only used in a couple of places and was easy to migrate.

from mockito.

fibulwinter avatar fibulwinter commented on July 20, 2024

Essentially a spy is a proxy to real object. It would return mock result if you ask for configured it previously, but for anything else it just forward call to real object. Real object has no access to spy, so it will use real methods for all calls.

In your example ft.calculate() will be delegated to real object (as there is no stubbing for it defined) and real object will use real myCalc().

from mockito.

zijam avatar zijam commented on July 20, 2024

Thx for the clarification 👍

from mockito.

EasonPai avatar EasonPai commented on July 20, 2024

So, without spy how can we do this now?

from mockito.

EasonPai avatar EasonPai commented on July 20, 2024

Thanks for the help.

I have thought about thenAnswer,
but it can be tedious when I have to do partial mocking in a big class.

Do we have any plan to add back spy api? or something like thenCallRealMethod()
http://static.javadoc.io/org.mockito/mockito-core/2.15.0/org/mockito/Mockito.html#partial_mocks

from mockito.

kjawadDeveloper1 avatar kjawadDeveloper1 commented on July 20, 2024

Any update on using Spy methods?

from mockito.

srawlins avatar srawlins commented on July 20, 2024

No; no current plans for supporting spies.

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.