From c6068db0cde9a31aa3adc12a1a5d6c16cfafef90 Mon Sep 17 00:00:00 2001 From: Chris James Date: Mon, 6 Jul 2020 08:20:36 +0100 Subject: [PATCH] better sources --- mocking.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mocking.md b/mocking.md index 661950138..c290a69d8 100644 --- a/mocking.md +++ b/mocking.md @@ -602,14 +602,12 @@ This is usually a sign of you testing too much _implementation detail_. Try to m It is sometimes hard to know _what level_ to test exactly but here are some thought processes and rules I try to follow: - **The definition of refactoring is that the code changes but the behaviour stays the same**. If you have decided to do some refactoring in theory you should be able to do make the commit without any test changes. So when writing a test ask yourself - - Am I testing the behaviour I want or the implementation details? + - Am I testing the behaviour I want, or the implementation details? - If I were to refactor this code, would I have to make lots of changes to the tests? -- Although Go lets you test private functions, I would avoid it as private functions are to do with implementation. +- Although Go lets you test private functions, I would avoid it as private functions are implementation detail to support public behaviour. Test the public behaviour. Sandi Metz describes private functions as being "less stable" and you don't want to couple your tests to them. - I feel like if a test is working with **more than 3 mocks then it is a red flag** - time for a rethink on the design - Use spies with caution. Spies let you see the insides of the algorithm you are writing which can be very useful but that means a tighter coupling between your test code and the implementation. **Be sure you actually care about these details if you're going to spy on them** -As always, rules in software development aren't really rules and there can be exceptions. [Uncle Bob's article of "When to mock"](https://8thlight.com/blog/uncle-bob/2014/05/10/WhenToMock.html) has some excellent pointers. - #### Can't I just use a mocking framework? Mocking requires no magic and is relatively simple; using a framework can make mocking seem more complicated than it is. We don't use automocking in this chapter so that we get: @@ -640,4 +638,8 @@ Martin Fowler. Once a developer learns about mocking it becomes very easy to over-test every single facet of a system in terms of the _way it works_ rather than _what it does_. Always be mindful about **the value of your tests** and what impact they would have in future refactoring. -In this post about mocking we have only covered **Spies** which are a kind of mock. There are different kind of mocks. [Uncle Bob explains the types in a very easy to read article](https://8thlight.com/blog/uncle-bob/2014/05/14/TheLittleMocker.html). In later chapters we will need to write code that depends on others for data, which is where we will show **Stubs** in action. +In this post about mocking we have only covered **Spies** which are a kind of mock. The "proper" term for mocks though are "test doubles" + +[> The generic term he uses is a Test Double (think stunt double). Test Double is a generic term for any case where you replace a production object for testing purposes.](https://martinfowler.com/bliki/TestDouble.html) + +Under test doubles, there are various types like stubs, spies and indeed mocks! Check out [Martin Fowler's post](https://martinfowler.com/bliki/TestDouble.html) for more detail.