Search code examples
testingmockingstubs

When should I stub out a type by manually creating a "stub" version, rather than using a mocking framework


Are there any circumstances where it is favourable to manually create a stub type, as opposed to using a mocking framework (such as Rhino Mocks) at the point of test.

We take both these approaches in our projects. My gut feel when I look at the long list of stub versions of objects is that it will add maintenance overhead, and moves the implementation of the stub away from the point of test.


Solution

  • For the purposes of automated unit tests, mocks are almost always the better option. Because they use reflection to mock an object, they will stay up-to-date when you make changes.

    The only time I create a stub is for integration testing - for example, you might stub an SMS Sending Service so you can run a period of integration / user testing without being charged for sending out SMS Messages (instead, the messages are stored so they can be checked).