Coming from minitest
The same ideas under other names, and a few left out on purpose.
minitest writes a test as a method that builds an object, acts on it and asserts what came out. Fix splits that in two. A specification says what must come out, in a document that names no class and holds no code that runs before or after an expectation; the test, on the last line, gives the subject, so that the same specification can test another one. The page speaks of test classes and assertions; for minitest/spec, with describe and it, Coming from RSpec is closer.
Structure
| minitest | Fix |
|---|---|
class CounterTest < Minitest::Test … end | Fix :Counter do … end |
def setup, @counter = Counter.new(0) | the block of the test: Fix[:Counter].test { Counter.new(start) } |
def test_count, then @counter.count in it | on :count do … end |
a value of setup, or a method of the test class | let(:start) { 0 }, visible from the subject block and the other lets only |
| another test, or another class, built with another value | with start: 5 do … end |
@counter.increment at the head of a test | on :increment do … end, around the expectations it prepares |
assert_equal 0, @counter.count | it MUST eq 0 |
refute_equal 0, @counter.count | it MUST_NOT eq 0 |
| a module of tests, included in several test classes | a specification of its own, required with fix(:Name), or tested against several subjects |
ruby test/counter_test.rb, rake test | ruby fix/counter_fix.rb, fix |
--seed 1234 | fix --random=1234, for the order of files |
A test class of a counter, with a setup, a test that increments it and another that builds it from another value:
require "minitest/autorun"
class Counter
attr_reader :count
def initialize(start) = @count = start
def increment = tap { @count += 1 }
end
class CounterTest < Minitest::Test
def setup
@counter = Counter.new(0)
end
def test_count
assert_equal 0, @counter.count
end
def test_count_after_an_increment
assert_equal 1, @counter.increment.count
end
def test_count_from_five
assert_equal 5, Counter.new(5).count
end
end
becomes:
require "fix"
class Counter
attr_reader :count
def initialize(start) = @count = start
def increment = tap { @count += 1 }
end
Fix :Counter do
let(:start) { 0 }
on :count do
it MUST eq 0
end
on :increment do
on :count do
it MUST eq 1
end
end
with start: 5 do
on :count do
it MUST eq 5
end
end
end
exit(Fix[:Counter].test { Counter.new(start) })
ruby counter_fix.rb counter_fix.rb:14: Success: expected 0 to eq 0 counter_fix.rb:19: Success: expected 1 to eq 1 counter_fix.rb:25: Success: expected 5 to eq 5
The setup became the block of the test, and the value it gave the counter a let, which a with changes for one context. The call that came before the assertion became a method of the chain: increment returns the counter, and count is sent to it.
Assertions
| minitest | Fix |
|---|---|
assert_equal x, v | eq(x), which wants the class of x and uses eql?, where assert_equal uses ==: 1.0 is not eq 1 |
assert_same x, v | be(x) |
assert_nil v, assert_equal true, v | be_true, be_false, be_nil |
assert v, refute v | satisfy { |value| !!value }, satisfy { |value| !value } |
assert_kind_of K, v | be_a_kind_of(K) |
assert_instance_of K, v | be_an_instance_of(K) |
assert_in_delta x, v, d | be_within(d).of(x) |
assert_match regexp, v | match(regexp), for a String or a Symbol |
assert_operator v, :>=, 3 | satisfy { |v| v >= 3 }, answering true or false |
assert_raises(K) { … } | raise_exception(K), the method of the context being what raises |
assert_difference, of Rails | change(o, :count).by(1) |
assert_empty v, assert_predicate v, :empty? | on(:empty?) { it MUST be_true } |
assert_includes v, x | on(:include?, x) { it MUST be_true } |
assert_equal "Ada", v.name | on(:name) { it MUST eq "Ada" }, or its(:name) { MUST eq "Ada" } |
Three levels instead of skip
minitest skips a test with skip: it is counted apart, and its assertions do not run. Fix says it in the specification itself: SHOULD for what is recommended, a warning when it is not met; MAY for what is optional, an info when the method does not exist. Both pass, both run, and both are written in the lines, so nothing is hidden. Writing specifications has the five outcomes.
Left out on purpose
setupandteardown: the subject block builds what a subject needs, for each expectation, and each context runs in a process of its own, so there is nothing to clean up in memory.- Mocks and stubs,
Minitest::Mock,stub: give the subject a real object, or a small object of the file that stands for a service, through a let. A specification says what a subject does, not which messages it sends. - Shared state between tests, instance variables, class variables: a let lives with one subject, and a context cannot reach another; only a
withshares its value, within its context. - A random order of tests: minitest shuffles the tests to find those that depend on one another. The contexts of Fix run in the order written, each in its process, where the order cannot change a result;
fix --randomshuffles the files, which share what is outside the processes. - Custom messages, the last argument of an assertion: every expectation gets its line, with what was expected and what came out.
Both can live in one project, and a test of minitest can check a specification of Fix: Beside RSpec or minitest tells how.