Red-green-refactor: the discipline, and the spec behind this course

Free preview 3 min to read

What this app's tests run

bundle exec rspec spec/services/identity/account_update_service_spec.rb
........

Finished in 0.27163 seconds (files took 1.61 seconds to load)
8 examples, 0 failures

That's the spec file this course follows, run for this course. Gemfile line 60 says gem "rspec-rails": this app is tested with RSpec, not Minitest. The discipline this course teaches works the same in either framework, and every example comes from this app's own code.

Three words, and what each one means

Red-Green-Refactor names three states a piece of code and its test can be in, in a fixed order, repeated. Red: a test exists and fails, on purpose, because the behavior it checks for doesn't exist yet. Green: the smallest change that makes that test pass, and nothing more. Refactor: changing the code's shape, names and structure without changing what it does. You do it only while the suite is green, so any behavior change shows up at once as a new failure.

The order is the whole discipline. Writing the test after the code already works skips Red, and skipping Red is this course's central problem, starting next lesson.

One pair of files, followed through all seven lessons

This course doesn't build a toy example. It follows one file this app already ships, app/services/identity/account_update_service.rb, and the spec that tests it, spec/services/identity/account_update_service_spec.rb. The service updates a user's name, email, time zone and password from the account settings form. Its spec has eight examples in four context blocks: basic info, time zone, password, and basic info and password together.

That last context exists because of a bug this file had, fixed in commit 999182a, "Fix: AccountUpdateService could partially commit on a rejected password change." The commit message states its testing approach: "Added regression specs proving the fix (and proving they fail against the previous version)." That sentence is Red-Green-Refactor in practice, and the next lessons walk through it.

What this course covers

Lesson 2 covers Red: why a test that never failed never proved anything, walked through against the bug and the regression test written for it. Lesson 3 covers Green, the smallest change that made it pass, using the commit's diff. Lesson 4 explains why Green only means something because Red happened first. Lesson 5 covers Refactor, done only under a green suite. Lesson 6 reads the spec's together-context as one Red-Green-Refactor story. Lesson 7 treats the cycle as a loop and points to where this app's testing courses go next.

Lesson 1 of 7 · free

Keep going with lesson 2

Red: a test that never failed never proved anything

Traces the pre-fix code and shows why the regression test fails against it, which is the Red step.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...