Fake-Mock-Stub

Fake - A fake is a generic term which can be used to describe either a stub or a mock object.

A fake can be a stub or a mock depending on the context in which it's used.


Mock - A mock object is a fake object in the system that decides whether or not a unit test has passed or failed. A mock starts out as a Fake until it is asserted against.


Stub - A stub is a controllable replacement for an existing dependency (or collaborator) in the system. By using a stub, you can test your code without dealing with the dependency directly. By default, a fake starts out as a stub.

Stub Example:

var stubOrder = new FakeOrder();
var purchase = new Purchase(stubOrder);

purchase.ValidateOrders();

Assert.True(purchase.CanBeShipped);

Mock Example:

var mockOrder = new FakeOrder();
var purchase = new Purchase(mockOrder);

purchase.ValidateOrders();

Assert.True(mockOrder.Validated);

The main thing to remember about mocks versus stubs:

Mocks are just like stubs, but you assert against the mock object,

whereas you do not assert against a stub.