Fake vs Mock vs Stub

When it comes to unit testing there are a lot of different terms and concepts, let’s check some of them.

Fake – A fake is a generic term that can be used to describe a stub or a mock object. Whether it’s a stub or a mock depends on the context in which it’s used. So, in other words, a fake can be a stub or a mock.

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

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

It is good to understand the subtle differences between these concepts, but most importantly, it is adamant to speak the same language among our team. This will reduce confusion and misunderstandings between developers, improve the codebase consistency, and promote a common code culture for the team.

By:

Posted in:

tags:


Leave a Reply

%d bloggers like this: