Thursday, November 17, 2011

Stubs vs Mocks


A stub is a piece of code that’s inserted at runtime in place of the real code, in order to isolate the caller from the real implementation. The intent is to replace a complex behavior with a simpler one that allows independent testing of some part of the real code.

Mocks replace real objects from the inside, without the calling classes being aware of it; They are objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
The most important point to consider when writing a mock is that it shouldn’t have any business logic. It must be a dumb object that does only what the test tells it to do. It’s driven purely by the tests. This characteristic is exactly the opposite of stubs, which contain all the logic.

http://martinfowler.com/articles/mocksArentStubs.html

No comments:

Post a Comment