Minitest in spec style
describe and it
Minitest ships two front ends. Minitest::Test is the class style of lesson 11.12; describe and it are the spec style, which reads as a sentence and groups tests by behaviour:
require "minitest/autorun" require_relative "../lib/calculator" describe Calculator do it "adds" do _(Calculator.new.add(2, 2)).must_equal 4 end it "raises on zero" do _(-> { Calculator.new.divide(1, 0) }).must_raise ArgumentError end end
Under the hood it is the same runner: each it becomes a test method, and the description becomes part of the name in the report. The style changes how the file reads, not what runs.
let
let defines a memoised helper: the block runs the first time the name is used in each test, and the value is fresh for the next one:
describe Calculator do let(:calc) { Calculator.new } it "adds" do _(calc.add(2, 2)).must_equal 4 end end
A test that never mentions calc never builds it, which is the advantage over setup and the reason a spec file with many let lines stays fast.
Expectations
An expectation wraps the value in _() and calls a matcher. The names read as a sentence and the failure message names the matcher:
| Expectation | Asserts |
|---|---|
| _(x).must_equal 4 | equal to 4 |
| _(x).must_be_nil | nil |
| _(x).must_be :empty? | the predicate is true |
| _(x).wont_be_empty | the negation |
| _(-> { f }).must_raise E | the block raises E |
wont_* is the negation family. A lambda is needed for must_raise because the call has to be lazy: an expression would raise before the matcher saw it.
Choosing a style
Both styles run in one project, and a suite with both is harder to read than either. Pick one per project:
- Class style. One class, one test class, explicit names: good for a library with a small public surface.
- Spec style. Grouped by behaviour, descriptions read as sentences: good for a project where the tests document the API.
The course uses the class style for library tests and the spec style here because RSpec in lesson 11.17, later, uses the same words. Mixing the two in one folder is the only choice that is clearly wrong.
What to watch for
must_equal on an object compares with
==; a class without ==
compares identity, and two equal-looking objects fail.
Lesson 2.6
has the rule and
lesson 6.9
the value object that fixes it.
A describe block with one test and five
let lines is a sign the setup is too broad. Narrow
the test until it needs one or two helpers.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
$calls = 0 value = nil def build $calls += 1 [] end value ||= build value ||= build puts $callsAnswer
1
The second ||= sees the value the first one stored, so the builder runs once, which is what let does per test.
-
What does this program print?
class Money attr_reader :amount def initialize(amount) @amount = amount end def ==(other) other.is_a?(Money) && amount == other.amount end end puts Money.new(5) == Money.new(5)Answer
true
The class defines ==, so must_equal compares the amounts rather than the identities.
-
What does this program print?
class Money attr_reader :amount def initialize(amount) @amount = amount end end puts Money.new(5) == Money.new(5)Answer
false
Without == the objects compare by identity, and two constructions are two objects.
-
What does
_(-> { f }).must_raise ArgumentErrorneed the lambda for?Answer
To make the call lazy, so the matcher can catch the error
A plain expression would raise before the matcher was reached, so the call must wait for it.
-
What does one
itblock become in the runner?Answer
One test method, with the description in its name
The spec style is a front end over the same runner, so each it is a test method.
-
When does a
let(:calc) { Calculator.new }block run?Answer
The first time the name is used in a test
The helper is lazy, so a test that never names it never builds it.
-
Which expectation is the negation of
must_be_empty?Answer
wont_be_empty
The wont_ family is the negation of the must_ family in the spec style.
-
Why does the lesson recommend one style per project?
Answer
A suite that mixes them is harder to read than either
Both run, and the choice is about how the file reads for the next person.
-
What does a failed
must_equalname in its message?Answer
The matcher and the values it compared
The matcher is part of the failure, which is why the expectation reads as a sentence.
-
What does
_(x).must_be :empty?assert?Answer
That x.empty? returns a truthy value
The symbol names a predicate, and the matcher calls it on the value.
Article author: Arthur Isaev