Test Doubles and Testing at a Boundary · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Stand-ins for awkward neighbours

Computing · Software Engineering · ages 20-21
Name ______________________   Date ____________
  1. A fake clock always says 9 in the morning. What kind of stand-in is it?

    • A mock, because it checks calls
    • A stub, because it returns a canned answer
    • A copy, because it runs the real logic
  2. A test replaces the database with something the test controls. What is that replacement called?

    • A stand-in behind a seam
    • A second copy of the database
    • A faster database server
  3. A mock records which calls it received so the test can check them.

    Circle one:   True   False

  4. You want to check that your code asked the database for user 42. Stub or mock?

    • A mock, because the test must inspect the call
    • A stub, because canned rows are enough
    • Neither, because databases cannot be replaced
  5. You test a function that reads the current time. Where do you put the seam and why there?

    • Inside the time formatting code, to test each line
    • Around the clock itself, so the test controls time
    • Inside the test runner, so all tests share one clock
  6. A refactoring renames two helper functions and five tests fail, though behaviour is unchanged. What broke?

    • The stand-ins returned the wrong canned answers
    • The seam was placed at the boundary
    • The tests assert on internal calls instead of results
  7. Ana replaces the network with a mock and then asserts only on the final message shown to the user. A teammate says the mock is pointless. Is it?

    • Yes, because mocks must always assert on calls
    • No, because the mock still removes the slow network
    • Yes, because only stubs can replace a network
  8. Two designs: the code builds its own database connection inside, or it receives one from outside. Which survives refactoring better, and why?

    • Receiving it, because the seam sits at the boundary
    • Building it inside, because there are fewer parts
    • Both equally, because seams do not matter
LightMySky · lightmysky.comW1-mt_4ep3SiMYNM-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Stand-ins for awkward neighbours W1-mt_4ep3SiMYNM-s1

  1. A stub, because it returns a canned answer · It only hands back a fixed answer, which is exactly what a stub does.
  2. A stand-in behind a seam · The test swaps the awkward neighbour for a stand-in it controls.
  3. True · Recording calls is what makes a mock more than a stub.
  4. A mock, because the test must inspect the call · Only a mock records calls, so only a mock can confirm the request.
  5. Around the clock itself, so the test controls time · Seaming the clock lets you freeze time while the logic stays untouched.
  6. The tests assert on internal calls instead of results · Call-watching tests couple to the implementation, so renames break them.
  7. No, because the mock still removes the slow network · The stand-in still controls the neighbour. Skipping call checks just keeps the test robust.
  8. Receiving it, because the seam sits at the boundary · An outside connection is a boundary seam, so internals can change freely.
Worksheet · LightMySky