Doubles and stubs
Why a double
A unit test should fail for one reason. A test of a class that calls a network client and fails on a slow day fails for two: the logic and the network. A double replaces the client with a value the test controls, so the only thing under test is the logic:
class Weather def initialize(client) = @client = client def forecast(city) = @client.get("/weather/#{city}") end
The constructor takes the client, which is the design that makes the test possible. A class that creates its own Net::HTTP inside the method cannot be tested this way, and that is the reason to inject the dependency.
Minitest::Mock
A mock states the call in advance: which method, what it returns and which arguments are allowed. verify fails the test when the call never happened:
require "minitest/autorun" require "minitest/mock" class WeatherTest < Minitest::Test def test_forecast_asks_the_client client = Minitest::Mock.new client.expect(:get, "sunny", ["/weather/Ada"]) assert_equal "sunny", Weather.new(client).forecast("Ada") client.verify end end
The mock checks the protocol, not just the answer: a wrong path or a missing call fails. Minitest 6 moved the mock into the minitest-mock gem, so the install is one more line on a modern Ruby:
gem install minitest-mock
1 gem installed
stub
stub replaces one method on a real object for the length of a block, then puts the original back. It is the lighter tool when the test only needs an answer, not a promise about the call:
def test_stub_replaces_one_method client = Object.new def client.get(_path) = "real" client.stub(:get, "rain") do assert_equal "rain", Weather.new(client).forecast("Ada") end assert_equal "real", client.get("/weather/Ada") end
The method must exist before it can be stubbed; the first version of this test created a bare Object.new and failed with undefined method 'get'. Defining the real method first is also more honest: the double stands in for something that has a shape.
The limits
A test full of mocks checks that the code calls the methods the test expects, not that the program works. Two rules keep it useful:
- Mock at the boundary. A client, a clock, a random source. Not the class under test's own private methods.
- Keep one integration test. The double proves the logic; one test against the real client proves the pair still fits.
The reader should be able to delete a mock and name the real collaborator it stands for. A mock that replaces a method invented by the test is a test of the test.
What to watch for
stub restores the method at the end of the block,
even when the block raises. A stub that leaks past the test makes the next
test pass for the wrong reason, and it is the reason the block form is
required.
A mock with a strict argument list fails when the code passes an extra
keyword. That is the point, and also the usual reason a harmless
refactor breaks a suite of mocks.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
class Weather def initialize(client) = @client = client def forecast(city) = @client.get("/weather/#{city}") end class FakeClient def get(path) "sunny for #{path}" end end puts Weather.new(FakeClient.new).forecast("Ada")Answer
sunny for /weather/Ada
The fake client returns the path it was given, and the Weather class passes the built path to it.
-
What does this program print?
class Weather def initialize(client) = @client = client def forecast(city) = @client.get("/weather/#{city}") end calls = [] client = Object.new client.define_singleton_method(:get) do |path| calls << path "sunny" end Weather.new(client).forecast("Ada") p callsAnswer
["/weather/Ada"]
The fake records the argument, which is what a mock expects in advance and verifies afterwards.
-
Why does the Weather class take its client in the constructor?
Answer
So the test can hand it a double instead of the network
Injection is what makes the logic testable without a network, and it is the design the double stands in for.
-
What does
client.expect(:get, "sunny", ["/weather/Ada"])promise?Answer
That get is called with that argument and returns that value
The mock fixes the method, the answer and the allowed argument, and a wrong path fails.
-
What does
client.verifydo?Answer
Fails when an expected call never happened
The expectation is a promise about the call, and verify is what makes the test check it.
-
How long does a
stubreplacement last?Answer
For the length of its block
The block form restores the original method, even when the block raises.
-
Why must the real method exist before it is stubbed?
Answer
The stub aliases the existing method, so an absent one fails
Defining the real method first also keeps the double honest: it stands in for something with a shape.
-
Where should a mock stand in?
Answer
At the boundary: a client, a clock, a random source
A test full of internal mocks checks the calls the test invented rather than the behaviour.
-
Why keep one integration test next to a suite of doubles?
Answer
Because the double proves the logic, and one real test proves the pair still fits
The double cannot notice that the real collaborator changed its shape.
-
What breaks when a mock has a strict argument list?
Answer
A harmless refactor that passes an extra keyword
The strictness is the point of a mock and also the usual reason a refactor breaks a suite of them.
Article author: Arthur Isaev