-
Notifications
You must be signed in to change notification settings - Fork 0
Test Double
Devrath edited this page Oct 25, 2023
·
4 revisions

Contents |
|---|
| What is a test double |
| What is the need for test double in unit testing |
| Types of test double |
| When to use mocks and when to use fakes |
A test double is a stunt double. It actually stands in the place of a real thing.

- We face many problems where a program is dependent on an external program or instance of an external program
- Example your
log-inscreen can be dependent on any eternal service likegoogle-login,facebook-loginetc ..
- We want to check scenarios like successful and failure in login and also need to check the login when the services are not available.
- We can't ask the services to behave differently just so that we want to test the test cases. Since it is not our concern how the service works.
- Using the test doubles we can substitute the
servicewith our own and change the behavior of it so that we can test our test cases.

- They are used when there is no easy way to verify a specific piece of code is executed.
- It is used when we need to test legacy code. Scenarios like when you don't have proper abstraction
- Testing the code that you do not own like third-party code that you cannot access
- It is used to verify real-world production behavior in a simplified way.
- One example is instead of using persistence storage, We use an in-memory list.
- Sometimes when you use mock and write the test cases, the production code gets extended with new functionalities
- The test cases might fail because the new functionalities are not mocked.
- But using a
fakeyou are forced to implement all the new implementations since the interface enforces that. - Overusing the mocks is always not good so just use it absolutely when necessary.
- So if the code is
legacy codeandcode that you do not ownwe can use the mocks else always go forfakes.