What is faking vs. mocking vs. stubbing?

Introduction

When writing tests, we often need to control the behavior of our dependencies. This is especially true when those dependencies are external services or APIs. To do this, developers use what are called test doubles.

Test doubles are objects that stand in for other objects during a test. There are different types of test doubles with different purposes:

Types of test doubles

Fakes

Fakes are objects that have working implementations. A fake object implements the same interface as a real object but takes shortcuts to improve performance. Fakes are generally used when we need to test something that depends on an external service or API, and we don’t want to make actual calls to that service.

Example

An in-memory database is a fake because it implements the same interface as a real database but doesn’t use disk storage. It makes it much faster than a real database, but it also means that the data is only persisted in memory and will be lost when the application restarts.

Mocks

Mocks are objects that have predefined behavior. These objects register calls they receive, allowing us to assert how we use them in the code. Unlike fakes, mocks don’t have working implementations. Instead, they have pre-programmed expectations about how they will be used in the code.

Example

A mock object might be programmed to return a specific value when it is called with certain arguments. Mocks are generally used to test the behavior of our code rather than its output. We can use mocks to verify that our code is calling the dependencies in an expected way.

Stubs

Stubs are objects that return predefined values. Like mocks, they don’t have working implementations. However, unlike mocks, they are not programmed to expect specific calls. Instead, they return values when they are called.

Example

A stub might be programmed to always return the same value when called with any arguments. Stubs are generally used to provide data that our code needs to run. This data can be hard-coded or generated dynamically.

Choosing a test double

When choosing a test double, we need to consider what we want to test and how we want to test it. Fakes are generally used to improve performance by avoiding external calls. Mocks are used to verify the behavior of our code. Stubs are used to provide data that our code needs to run.

We should use the simplest test double that will get the job done. For example, if we want to test the output of our code, we can use a stub. If we’re going to test the behavior of our code, we can use a mock.

Conclusion

Test doubles are objects that stand in for other objects during a test. There are different test doubles with different purposes—fakes, mocks, and stubs. Fakes are objects that have working implementations. On the other hand, mocks are objects that have predefined behavior. Lastly, stubs are objects that return predefined values. When choosing a test double, we should use the simplest test double to get the job done.

Free Resources