Mocks Aren’t Stubs

I’ve just read a really interesting article by Martin Fowler Mocks Aren’t Stubs‘. This lead me to the conclusion that I have also been confusing the terms mocks and stubs. To try and summarise the key difference between mocks and stubs is chiefly that of testing behaviour and/or state. Testing state means that you are only interested in the final outcome of the objects and their eventual state, and not all how they reached it; Testing behaviour means that you are instead interested in the behaviour of the objects and how they got to their final state. A stub will not, in general, support testing of behaviour, whereas a mock is fundamentally all about behaviour testing.

So in my previous post “Mocking without a mocking framework” I was really talking about stubbing. Specifically combining Dependency Injection with (dynamic?) stubbing to support testing.

I think a really interesting question is “do you want behaviour testing?” It seems unnatural for a test to care how an object came up with its answer.


About Alex McMahon

I am a software developer, interested in .net, agile, I've previously specialised with .net 3.0 technologies like WCF, whereas now I am trying to specialise in agile development and best practice and patterns. I am obsessed with looking at the latest technologies, tools, and methods, and trying them out. I am currently employed by Rockwell Collins in the UK.
This entry was posted in dependency injection, IoC, mocks, TDD, testing. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s