Archive for February, 2008

A Time To Stub, A Time To Mock

Tuesday, February 5th, 2008

Often, there’s an implication that everything being unit tested is on an equal footing. That isn’t the case. I think within unit testing there is a spectrum. On one end you may have a simple object on the other end you may have a more complex object composed of several other objects. For the simple object, state-based testing is easy and understandable. For the composed object, behavior-based testing becomes preferable.

Expanding on this, I’ve noticed that when I am just starting development, there are no mocks or stubs. As I progress, there are instances where I am using some stubs in tests. And when I almost have functionality, I am using mocks when testing. I use stubs at a “lower” level and mocks at a “higher” level.

I think that this makes sense. Once I’ve got my base set of objects, I just want to make certain that they talk to each other in the appropriate manner. I trust that the individual objects know what to do. At that higher level, it’s the interaction I want to test.