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?