Should I practice "mockist" or "classical" TDD?

A very pragmatic approach was exposed by Sandi Metz:

An object can communicate to other object through outgoing or incoming messages. The messages can be queries (returns something) or commands (executes something).

There are four combinations. The outgoing query messages not should be tested (already tested as incoming query of external class) You could to use the mockist test method on outgoing command messages and classical test to the rest.

enter image description here

Check the links

http://jnoconor.github.io/blog/2013/10/07/the-magic-tricks-of-testing-by-sandi-metz/

https://speakerdeck.com/skmetz/magic-tricks-of-testing-ancientcityruby

Youtube


I don't think you need to choose one over the other. Both have their advantages and disadvantages and both are tools for your toolbox. "Mockist" tdd makes you a bit more flexible in what you can test while classical TDD makes your tests a bit less brittle because they tend to look more at the input/vs output instead of looking at the actual implementation. When doing mockist unit testing I seem to have more tests break when changing the implementation.

I try to use classical tdd whenever possible (although i often use a mocking framework to set up the stubs quickly). Sometimes I notice I start testing too much at one time or i need too many objects to set up a test. That's when mockist testing can often help you set up smaller tests.

This is all quite abstract so I hope i make sense


You might consider looking at our book, at http://www.growing-object-oriented-software.com/. It includes an extended worked example. As we wrote it we discovered that the state vs. interaction distinction is largely misleading and it's more about one's approach to OO design.


The question about mockist or classic tdd is very much about what part of your application you're testing. If you have a "standard" layered architecture (like DDD for example) the domain layer is usually suited for classic tdd where you unit test by setting up the object under test, call a few methods and check the result and/or the state.

On the otherhand when you're testing application services, controllers or presentation logic which all do more coordinating work, mocking or stubbing is often needed to get good tests. My experience is also that these classes tend to call other layers (webservice, datalayer,...) which you really want to mock or stub. These unit tests also need more setup code so you should only mock when you have to.

My advice is to go classic whenever you can and mock when you must.

Tags:

Tdd

Mocking