When to use Mock v. Stub or not?

I read on Mocks and Stubs, their difference and usage. I'm still a little confused, but I think I have it.

Now I'm curious about applications. I can see use when creating โ€œfakeโ€ objects in test scripts, where the actual objects are too complex to drag and drop to test one aspect.

But consider my application: I am working on a computational geometry library. Our library defines points, lines, lines, vectors, polygons and polyhedra, as well as many other objects and all the usual geometric operations. Any given object is stored as a list of points or directions or lower level objects. But not one of these objects generates more than a few milliseconds.

When I test this library, does it make sense to use Mocks / Stubs anywhere?

Now we just use specific test cases. We call them stubs, but I donโ€™t think they meet the technical definition of stubs. What do you think will be better for this? "TestCases"? "Examples"?

SourceCode: https://bitbucket.org/Clearspan/geometry-class-library/src

Edit: note that we strive for immutability in all our geometry objects, so it makes sense to test the results of operations, but not change the state of the original objects.

+7
c # unit-testing mocking stubs
source share
2 answers

The fundamental difference between a mock and a stub is that the layout can cause your test to fail. Pieces cannot. Pieces are used to ensure the correct flow of the program. This is never part of the statement.

Note that the layout can also be used to provide flow. In other words, each layout is also a stub, and a stub is never a mockery. Due to such overlapping responsibilities, you currently don't see much of the difference between layouts and template developers and wireframes for more general terms (e.g. fake , substitute or catch-all mock ).

This implementation (mock-assert, stub-flow) helps us narrow down some usage scenarios. For starters, it's easier ...

Mock

As I said before, allegations use bullying. When the expected behavior of your component is that it should talk to this other component - use mock. All these

emailSender.SendEmail(email); endOfDayRunner.Run(); jobScheduler.ScheduleJob(jobDetails); 

can only be checked with the query "Did he call ScheduleJob with such and such parameters?" This is where you go to taunt. Usually this will be just a fake use case.

Stub

With a stub, this is slightly different. Whether or not to use a stub is a design issue . After you follow the regular loosely coupled dependency-based design, you end up with a lot of interfaces .

Now that in the test, how to return the value from the interface? You either close it or use a real implementation. Each approach has its pros and cons:

  • with stubs created by the library, your tests will be less fragile, but may require more work before you start (setting up stubs, etc.)
  • with real implementations, the configuration work has already been completed, but when Angle changes to the CoordinateSystem class may fail ... Is this behavior desirable or not?

It? Which one to use? Both! It all depends on...

Unit of work

We have come to the final and actual part of the problem. What is the area of โ€‹โ€‹your unit test? What is unit ? Can the CoordinateSystem be separated from its internal workings and dependencies ( Angle , Point , Line ) and can they be enveloped? Or more importantly, should they be?

You always need to determine what your device is . Is he the CoordinateSystem alone or perhaps Angle , Line and Point play an important role in it? In many, many cases, the unit will be formed both by the method and its surrounding ecosystem , including domain objects, auxiliary classes, extensions, or sometimes even other methods and other classes.

Naturally, you can separate them and drown them all the way, but then ... is this really your unit?

+3
source share

Typically, use Mocks when you need to model behavior and stubs when the only thing that matters in your test is the state of the object you are communicating with.

Considering the editing you made at your post, when you need to get an immutable object, use a stub, but when you need to invoke the operations that the object provides, then go to the layout so you are not prone to test failures due to errors in another class implementation.

+3
source share

All Articles