Mocks and stubs

Mocks and stubs are both similar because they are used to replace class dependencies or collaborators and most mocking frameworks provide the features to create both. Stubs can be hand-rolled in the same manner you hand-roll mocks.

So what really differentiates mocks from stubs? Mocks are used to test collaboration. This includes verifying the expectations of the actual collaborator. Mocks are programmed to have the expectation containing the details of method invocations to be received, while stubs are used to simulate collaborators. Let's explain this further with an illustration.

A stub can be used to represent the result from a database. In place of a database call to return a set of data, a C# list can be created with ...

Get C# and .NET Core Test Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.