# Test double patterns

**Test Double:** How can we verify logic independently when code it depends on is unusable? How can we avoid Slow Tests? \
We replace a component on which the SUT depends with a “test-specifi c equivalent.”

## Test Stub

We replace a real object with a test-specifi c object that feeds the desired indirect inputs into the system under test.

![](https://3408508746-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lis5JLcnSD2vdYdyQ3U%2F-Lk8yCWk81ab6wKLBxXf%2F-Lk9-DDOhDzpCPUV1H5t%2Fimage.png?alt=media\&token=5e323ca9-f582-4a56-b341-79b3ba6dae68)

## Test Spy

We use a Test Double to capture the indirect output calls made to another component by the SUT for later verification by the test.

![](https://3408508746-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lis5JLcnSD2vdYdyQ3U%2F-Lk8yCWk81ab6wKLBxXf%2F-Lk9-iMztKpWE7iRrft-%2Fimage.png?alt=media\&token=b59b6f62-f986-4b2f-8a06-e043ffe75941)

## Mock Object

We replace an object on which the SUT depends on with a test-specifi c object that verifi es it is being used correctly by the SUT

![](https://3408508746-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lis5JLcnSD2vdYdyQ3U%2F-Lk8yCWk81ab6wKLBxXf%2F-Lk9-vjfCs-Uekxe8tTF%2Fimage.png?alt=media\&token=7c68cfe2-e3c0-419e-92a6-b010e735c1a9)

## Fake Object

We replace a component that the SUT depends on with a much lighter-weight implementation.

![](https://3408508746-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lis5JLcnSD2vdYdyQ3U%2F-Lk8yCWk81ab6wKLBxXf%2F-Lk908ET0ERmt0TVbFT4%2Fimage.png?alt=media\&token=f09da07b-2843-491a-93d1-6f994cc10a1e)

## Configurable Test Double

We confi gure a reusable Test Double with the values to be returned or verifi ed during the fi xture setup phase of a test.

![](https://3408508746-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lis5JLcnSD2vdYdyQ3U%2F-Lk8yCWk81ab6wKLBxXf%2F-Lk90O6OcoNeCiA7qwID%2Fimage.png?alt=media\&token=6e8f6955-503b-47f1-b468-4696c7b56a81)

## Hard-coded Test Double

We build the Test Double by hard-coding the return values and/or expected calls.

![](https://3408508746-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lis5JLcnSD2vdYdyQ3U%2F-Lk8yCWk81ab6wKLBxXf%2F-Lk90a0pnFpmaC23yqlw%2Fimage.png?alt=media\&token=ae0baec1-818a-4fdb-9839-4f8861fc4c78)
