A4.1 · Specification by Example · dive 1
Examples as specification
The concrete examples are not an illustration of the spec. They are the spec — and the acceptance harness reads its checks straight from them.
An abstract requirement leaves the reader to guess; a worked example fixes one reading and
holds the build to it. Switch the view on the right to watch “handle a request with no signed-in
learner” collapse from three readings to one — the protected /my/courses
example: an unauthenticated request is redirected, never shown another learner's courses.
Abstract requirement, or a worked example · switch the view
What a worked example specifies
A worked example is a single concrete case, stated as Given/When/Then. “Given a request with no signed-in learner, when GET /my/courses, then it redirects.” There is nothing left to interpret: the case names the input, the action, and the exact result. Where the abstract sentence admitted a 404, a crash, and showing another learner's courses, the example admits one outcome.
That is the inversion Specification by Example makes. The specification is not a paragraph the examples decorate; the examples are the specification, and the prose around them only explains why. For one rung, the set of worked examples is the precise definition of what the rung does — precise because each example is concrete, and complete because the set is chosen to cover the behaviour.
- worked example
- A single concrete case as Given/When/Then. It pins exactly one behaviour, so nothing is left to read into it.
- the example set
- The chosen cases that together specify a rung. For the /my/courses page: an enrolled learner, a learner with no enrollments, an unauthenticated request.
- abstract requirement
- A sentence the reader has to interpret. It admits many readings; the example set narrows them to one.
- the harness
- The acceptance run. It reads its checks from the examples — no example, no check; no check, no proof.
The abstract requirement and the example set describe the same rung. The requirement is the loose face anyone can argue with; the example set is the precise face the build is held to.
The example is the check
Because a worked example is concrete, it does double duty: it specifies the behaviour and it accepts it. Run the example against the system and one of two things happens — the system produces the stated result, or it does not. There is no third reading to dispute. The example that pinned the meaning is the same artifact the harness runs to prove it.
Pick one of the three /my/courses examples below. The figure renders its
Given/When/Then and reports the case it realizes, drawn from the stories of
F6.5 · Views with HEExThe HEEx rung that server-renders the catalog and reconciles the learner route into the protected /my/courses.View the spec ladder →. The
example is the check: each case is one row the acceptance run evaluates.
The /my/courses examples · pick one
A worked example pins the behaviour and proves it — the same case the spec carries is the row the harness runs.
On the Portal
On the Portal — the learning platform this course builds — a learner reads their own enrolled
courses at the protected route /my/courses; behind it,
Portal.Enrollment returns the courses the signed-in learner is enrolled in. The route
is protected: a learner cannot see another learner's enrolled courses. The spec does not say “handle the
cases.” It states three worked examples, and they are the specification.
The fragment below is the stories text for the /my/courses page, settled at
F6.5 · Views with HEExThe HEEx rung that server-renders the catalog and reconciles the learner route into the protected /my/courses.View the spec ladder → — the Given/When/Then for the three cases. It is spec text,
not code: the example is what the harness reads.
# /my/courses — "See my courses" (verbatim acceptance, F6.5). # Given a signed-in learner with enrollments, # when GET /my/courses, then the page renders their own courses. # Given a signed-in learner with no enrollments, # when GET /my/courses, then it renders an empty state (not an error). # Given a request with no signed-in learner, # when GET /my/courses, then it redirects — a learner never sees another's courses. # Each example is one row the acceptance run evaluates. example_1 = "enrolled learner → their own courses" # the read case example_2 = "no enrollments → empty state" # the empty case example_3 = "not signed in → redirected" # the access case the_spec = :the_three_examples # the examples ARE the specification
The principle
The concrete examples are the specification, and the acceptance harness reads its checks from them. The prose explains; the examples define and prove.
On the Portal
The Portal defines its /my/courses page by three worked
Portal.Enrollment examples — an enrolled learner, one with no enrollments,
an unauthenticated request — and the acceptance run runs each as a check.
For one rung, the worked examples are the precise definition and the executable acceptance at once — one artifact, two jobs.
Recap — the examples are the spec
A worked example is a concrete Given/When/Then that pins one behaviour; the set of examples is the precise
definition of a rung; and because each example is concrete, it doubles as the check the harness runs. The
Portal's /my/courses page is specified by three Portal.Enrollment
examples, and the acceptance run reads its checks from them. The next dive draws the consequence: when the
examples are the checks, the documentation cannot drift from the system.
Next in A4.1: A4.1.2 · Living documentation — why documentation that is executed, not asserted, cannot fork from the system it describes.
References
The sources behind examples-as-specification, and where it connects in the course.
Sources
- Adzic, G. — Specification by Example — concrete examples as the executable specification.
- Gherkin reference — Given/When/Then as the structured form a worked example takes.
- Cohn, M. — User Stories Applied — the acceptance that closes a story.
Related in this course
- A4.1 · Specification by Example — the module hub.
- A4.1.2 · Living documentation — the next dive.
- A4 · The spec layer — the chapter.
- A4 · The spec ladder — the F6.1–F6.6 rungs these examples cite, across the iterations git recorded.
- A2.04 · Acceptance with Given/When/Then — the form an example is written in.
- A0.2.2 · The four artifacts — the spec a rung's examples define.
- Companion · Phoenix (F6) — the real chapter whose F6.5 rung grounds this dive's /my/courses examples.