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

StackOverflow https://stackoverflow.com/questions/2453460

  •  20-09-2019
  •  | 
  •  

Question

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.

Was it helpful?

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).

OTHER TIPS

When you reach the point where stubs take up too much maintenance time, it's time to move on to a stub/mock framework. So I think you have given the answer already yourself.

Currently I'm still using stubs with my current project, for the sake of simplicity. There are only a few types for which I use a stub, and usually there's only one stub, sometimes two to recreate errors, but no more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top