Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

tdd

mocking

I've read (and re-read) Martin Fowler's Mocks Aren't Stubs. In it, he defines two different approaches to TDD: "Classical" and "Mockist". He attempts to answer the question "So should I be a classicist or a mockist?", but he admits that he has never tried mockist TDD on "anything more than toys." So I thought I'd ask the question here. Good answers may repeat Fowler's arguments (but hopefully more clearly) or add arguments that he didn't think of or that others have come up with since Fowler last updated the essay back in January 2007.

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

Daryl Spitzer


People also ask

What's the difference between faking mocking and stubbing?

There are different test doubles with different purposes—fakes, mocks, and stubs. Fakes are objects that have working implementations. On the other hand, mocks are objects that have predefined behavior. Lastly, stubs are objects that return predefined values.

What is the relevance of mocking and stubbing in TDD?

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 stubbing and mocking?

Stubbing and Mocking are two fundamental approaches in the testing field. However, they are frequently misapprehended. There is a common perception that stubbing and mocking happen only for unit and component testing. But we can use them for other testing types as well.


1 Answers

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

like image 67
Mendelt Avatar answered Oct 14 '22 09:10

Mendelt