Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 38 ยท Testing and Patterns

Test-Driven Development in the Debugger

Follow the Smalltalk test-driven loop: write a failing test, let the debugger create missing methods, make it green, refactor. Worked example in read-only Smalltalk plus a runnable JavaScript simulation.

Test-driven development (TDD) has three beats: red (write a test that fails), green (write the least code that passes), refactor (clean up with the tests as a safety net). Smalltalk gives the loop an unusual shortcut. When a test calls a method that does not exist yet, the debugger opens at that exact line and offers to create the method. You write the body, press Proceed, and the test continues. You never leave the test.

This lesson walks one small feature through that loop. Smalltalk does not run on this site, so each step is shown read-only. A JavaScript simulation at the end models the "unknown message opens a hook that writes the method" behaviour so you can experiment.

The feature: a shopping cart

We want a cart that adds items with a price and quantity, and reports a total. Start with the test, in CartTest (read-only):

SMALLTALK
CartTest >> testEmptyCartTotalIsZero
    | cart |
    cart := Cart new.
    self assert: cart total equals: 0

Step 1: red

Run the test. The class Cart does not exist, so the debugger opens: "Cart is undefined". Choose Create class, pick a package and a superclass (Object). Press Proceed. Next the debugger stops again with "Cart doesNotUnderstand: #total". Click Create, choose the protocol accessing, and the debugger shows an empty method template.

Step 2: green, with the smallest code

Type the simplest thing that passes:

SMALLTALK
Cart >> total
    ^ 0

Accept it and press Proceed. The test is green. Returning a constant looks silly, but it is deliberate: the next test will force a real implementation.

Step 3: triangulate with a second test

SMALLTALK
CartTest >> testTotalSumsPriceTimesQuantity
    | cart |
    cart := Cart new.
    cart add: 'pen' price: 2 quantity: 3.
    cart add: 'book' price: 10 quantity: 1.
    self assert: cart total equals: 16

Run it. The debugger stops on add:price:quantity:, which does not exist. Create it. The new method needs somewhere to keep the lines, so you also add an instance variable and an initialize (read-only):

SMALLTALK
Object subclass: #Cart
    instanceVariableNames: 'lines'
    package: 'Shop'.

Cart >> initialize
    super initialize.
    lines := OrderedCollection new

Cart >> add: aName price: aPrice quantity: aCount
    lines add: { aName. aPrice. aCount }

Cart >> total
    ^ lines inject: 0 into: [ :sum :line | sum + (line second * line third) ]

Accept each method inside the debugger, then press Restart on the test frame. Both tests pass.

Step 4: refactor

The { name. price. count } array is hard to read. Introduce a small CartLine class with subtotal, and let total ask each line. Because both tests stay green throughout, you can make the change in tiny steps and run them after every step. The next lesson on design patterns shows other shapes this refactoring can take.

flowchart LR
  R["RED<br/>write a failing test"] --> D["debugger opens<br/>at the unknown message"]
  D --> C["Create method<br/>write smallest body"]
  C --> G["GREEN<br/>Proceed or Restart"]
  G --> F["REFACTOR<br/>keep the tests green"]
  F --> R

The debugger shortens the loop: you write code exactly where the test needs it.

Simulating the loop

The JavaScript stand-in uses a Proxy as a "missing method handler": when a test calls something unknown, the simulated debugger asks a recorded table for a body (standing in for what you would type) and installs it, then continues. It is only a model; the real debugger shows a window, not a table.

JS Stand-in: a debugger that creates missing methods

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that the second test is green even though add was created only when it was first needed, just as in the real loop.

Good habits in the loop

  • Keep each step small. If the debugger session takes more than a few minutes, the test is too big.
  • Name tests by behaviour (testEmptyCartTotalIsZero), not method (testTotal).
  • Run the whole package after every refactoring step; SUnit makes it a keystroke.
  • When a test is hard to write, the design is probably hard to use. Treat that as feedback.
  • Delete temporary code such as self halt before you commit with Iceberg.
JS Stand-in: triangulation forces generality

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three beats of TDD?

    Write a failing test, make it pass simply, then clean up with the tests as a net.
  2. 2. What does the debugger's Create button do for a missing method?

    You write the method in place and Proceed or Restart the frame.
  3. 3. Why return a constant in the first green step?

    Triangulation with a second example makes the constant fail and drives a real implementation.
  4. 4. When is refactoring safe?

    Green tests are the safety net that lets you restructure confidently.

Summary

  • Write a failing test, let the debugger create the missing class or method, then make it pass with minimal code.
  • Triangulate with a second test, then refactor while tests stay green.
  • Keep steps small and run the package tests after every change.
  • The Smalltalk sessions are read-only walk-throughs; the JavaScript examples are simulations.

Next: Design Patterns in Smalltalk