When you need a stub

In this exercise you need to use a stub to test the code.

There is a video on Emily Bache’s YouTube channel that goes through this learning hour “Using a Test Double to find bugs”.

Session Outline

Connect: How can you test this?

Today’s exercise is The Doppelganger kata, and in particular the Calculator problem. Read the code - the Calculator class. It contains a bug, marked with a comment. How could you write a test that would fail because of this bug? Discuss in pairs. Don’t write any code at this point, just talk.

Concept: Test Doubles

Explain what a test double is and how we could use one in this problem. Draw a picture like this:

Test Double for Calculator

You could explain that this particular kind of test double is a stub.

Concrete: Write a test using a test double

Ask people to write a test using a test double. It should fail because of the bug, and pass if you change the code to fix the bug. Do not change the Calculator class otherwise.

Conclusions: How do you know when you need a test double?

Why did we need a test double in order to test this code? Ask When should you use this?