POSTS
Test Doubles
By Carlos Buenosvinos
- 2 minutes read - 313 wordsLast week, another awesome cleancoders episode was published. It comes in two parts. I really recommend to buy it. Let’s review the most important concept taught in the first one: Test Doubles.
Favourite topic
In the video you can learn different cool thing but I’m going to sum up the concept I liked the most, test doubles types.
Test doubles help us testing your application in order not to couple our tests to our implementation so our tests do not become fragile.
Different test doubles are: Dummy, Stub, Spy, Mock and Fake. Generally speaking, we tend to call to all of these mocks, but the correct term is test double.
Dummy
It’s an object its functions do nothing and return null (or what’s near to null). I mean, it does nothing and returns nothing useful.
Typically, it’s an object you have to pass as a parameter to a function that it’s not going to do nothing with it, specially for the pathway testing.
Stub
A stub is a dummy test double that does nothing and its functions return anything you like in order to force the pathway you are testing. Your stubs will be shared between your different tests.
Spy
A spy is a stub that remembers some data that can be accessed from your tests. Interesting things to remember are functions calls (argument, number of invocations, etc.). They watch and remember.
Mock
A mock is a spy that knows what to expected. Tests ask the mock about what to expected.
Fake
Fakes are objects, not related with any of the previous ones, that encapsulates some logic in order to behave as another object. It’s like a simulator. For integration tests, fakes can be really useful. As a general recommendation, you should avoid fakes as possible. For your unit tests, stubs and spies should be enough.
Book recommendation
xUnit Test Patterns: Refactoring Test Code