Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

As a "mockist" TDD practitioner, should I mock other methods in the same class as the method under test?

Tags:

tdd

mocking

After reading Martin Fowler's Mocks Aren't Stubs, I've discovered I've been practicing TDD in the "mockist" fashion.

But I'm wondering if even in mockist TDD if one can take mocking too far.

Here's an updated example in Python-style pseudo-code:

def sync_path(self):
    if self.confirm_or_create_connection():
        self.sync(self.dirpath)

The confirm_or_create_connection() method creates a connection to a server.

I tested a method similar to this in two tests, both of which mock confirm_or_create_connection() and sync() (even though they're both methods in the same class). In one test the mock confirm_or_create_connection() returns True and the test confirms that sync() was called, and in the other the mock confirm_or_create_connection() returns False and the test confirms that sync() was not called.

Is this reasonable? Or should I mock the objects that confirm_or_create_connection() and sync() call? (I have other tests of both of these methods that already do this.)

Please don't answer the question by explaining that I should be practicing "classical" TDD instead. That's an answer to another question: Should I practice mockist or classical TDD?

like image 294
Daryl Spitzer Avatar asked Oct 08 '08 20:10

Daryl Spitzer


People also ask

What's the difference between faking mocking and stubbing?

Fakes are generally used to improve performance by avoiding external calls. Mocks are used to verify the behavior of our code. Stubs are used to provide data that our code needs to run. We should use the simplest test double that will get the job done.

What is mocking in TDD?

Mocking is the creation of an object that mimics another and sets expectations concerning its behavior. In testing, mocking replicates the behavior of a service, object, or process. There are a number of benefits that result from replacing an object with a mock for testing purposes.

What is the relevance of mocking and stubbing in TDD?

Mocking and stubbing of internal functions Mocks and stubs are very handy for unit tests. They help you to test a functionality or implementation independently, while also allowing unit tests to remain efficient and cheap, as we discussed in our previous post.

What is the difference between classical and Mockist testing?

Mocks use behaviour verification. Classical uses state verification. I generally prefer the classical style to the mocking mostly because I don't like thinking about implementation when writing a test.


2 Answers

The technique is called "mock objects", not "mock methods" for a reason. It encourages designs that divide the system into easily composed, collaborating objects and away from procedural code. The aim is to raise the level of abstraction so that you mostly program by composing objects and rarely write low-level control flow statements.

like image 179
Nat Avatar answered Oct 01 '22 15:10

Nat


Personally I think that mocking on self is almost always a code smell. It's testing the implementation rather than the behavior.

like image 35
Avdi Avatar answered Oct 01 '22 13:10

Avdi