Fixrb

Fix guide, version 1

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

minitestFix
class CounterTest < Minitest::Test … endFix :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 iton :count do … end
a value of setup, or a method of the test classlet(:start) { 0 }, visible from the subject block and the other lets only
another test, or another class, built with another valuewith start: 5 do … end
@counter.increment at the head of a teston :increment do … end, around the expectations it prepares
assert_equal 0, @counter.countit MUST eq 0
refute_equal 0, @counter.countit MUST_NOT eq 0
a module of tests, included in several test classesa specification of its own, required with fix(:Name), or tested against several subjects
ruby test/counter_test.rb, rake testruby fix/counter_fix.rb, fix
--seed 1234fix --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

minitestFix
assert_equal x, veq(x), which wants the class of x and uses eql?, where assert_equal uses ==: 1.0 is not eq 1
assert_same x, vbe(x)
assert_nil v, assert_equal true, vbe_true, be_false, be_nil
assert v, refute vsatisfy { |value| !!value }, satisfy { |value| !value }
assert_kind_of K, vbe_a_kind_of(K)
assert_instance_of K, vbe_an_instance_of(K)
assert_in_delta x, v, dbe_within(d).of(x)
assert_match regexp, vmatch(regexp), for a String or a Symbol
assert_operator v, :>=, 3satisfy { |v| v >= 3 }, answering true or false
assert_raises(K) { … }raise_exception(K), the method of the context being what raises
assert_difference, of Railschange(o, :count).by(1)
assert_empty v, assert_predicate v, :empty?on(:empty?) { it MUST be_true }
assert_includes v, xon(:include?, x) { it MUST be_true }
assert_equal "Ada", v.nameon(: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

Both can live in one project, and a test of minitest can check a specification of Fix: Beside RSpec or minitest tells how.