Fixrb

Fix guide, version 1

Beside RSpec or minitest

A specification of Fix in the suite a project already has, and the suite kept.

A project that has its tests in RSpec or minitest does not replace them to use Fix. A specification is added to the suite as it is: first checked from a test of the suite, then, when there are more of them, in a directory of its own that the suite runs. Nothing of the suite changes, and taking Fix out again is removing a line of the Gemfile and a few files.

A specification in a test

The project below has a class, in lib:

class Duck
  def walk = "Klop klop!"
  def swim = "Splash!"
  def speak = "Quaaack!"
end

Its specification is a document, in a file of its own that requires nothing but Fix. Here it is fix/specs/duck.rb, since spec is often the directory of RSpec:

require "fix"

Fix :Duck do
  on :walk do
    it MUST eq "Klop klop!"
  end

  on :swim do
    it SHOULD eq "Swoosh..."
  end

  on :speak do
    it MAY eq "Quaaack!"
  end
end

With minitest-fix, a test of minitest checks it with one assertion:

require "minitest/autorun"
require "minitest/fix"
require_relative "../fix/specs/duck"
require_relative "../lib/duck"

class DuckTest < Minitest::Test
  include Minitest::Fix

  def test_duck_meets_its_specification
    assert_fix :Duck, Duck.new
  end
end

With rspec-fix, an example of RSpec checks it with one matcher:

require "rspec/autorun"
require "rspec/fix"
require_relative "../fix/specs/duck"
require_relative "../lib/duck"

RSpec.configure { |config| config.include RSpec::Fix }

RSpec.describe Duck do
  it { is_expected.to fix(:Duck) }
end

The first line of each, minitest/autorun and rspec/autorun, lets ruby run the file as it is; a suite has it in its helper, or leaves it to its command. Both pass: the duck walks as required. It does not swim as recommended, and neither says so: a SHOULD not met is a warning, which passes, and an assertion or a matcher has no line to write it on. When a MUST is not met, the message of the failure is made of the lines of Fix, one for each expectation not met, each pointing at its line of the specification. A specification checked this way is written without with: the test gives a value, once, and the contexts cannot build it anew.

One command

A test of Fix writes every line, the warning among them. It is a file of fix, which requires the same document and ends with the status of the test:

require_relative "specs/duck"
require_relative "../lib/duck"

exit(Fix[:Duck].test { Duck.new })
fix
fix/specs/duck.rb:5: Success: expected "Klop klop!" to eq "Klop klop!"
fix/specs/duck.rb:9: Warning: expected "Splash!" to eq "Swoosh..."
fix/specs/duck.rb:13: Success: expected "Quaaack!" to eq "Quaaack!"
1 file met

Running specifications has the command. A project then has two commands, which a Rakefile runs one after the other: Next to other tests. Or the suite runs the files of fix itself, each as one of its tests, and there is one command and one report again:

require "minitest/autorun"
require "rbconfig"

# Every file of fix/ is a test of this suite: passed when it exits with 0,
# and what it wrote is the message when it does not.
class FixTest < Minitest::Test
  Dir.glob(File.join(__dir__, "..", "fix", "**", "*_fix.rb")).each do |file|
    define_method("test_#{File.basename(file, ".rb")}") do
      output, = capture_subprocess_io { system(RbConfig.ruby, file) }
      assert_predicate $?, :success?, output
    end
  end
end

Each file runs in a process of its own, as fix runs it, and what it wrote is the message of the test when its status is not 0.

The process of the test

In a test of Fix, the block of test builds the subject, and each context runs in a child process. Here the test of the suite has built the value, in its own process, where its transaction, its mocks and its coverage tool are: assert_fix and fix send the methods of the specification to that value, there. A method that changes the value changes it for the contexts that follow, and for the rest of the test.

Both take fork: true, as in assert_fix :Duck, duck, fork: true and fix(:Duck, fork: true), to run each context in a child process, as Fix does: the methods are sent to a copy of the value, and the one the test holds is left as it was. It is for a value whose state is all in the process. A mock or a double does not see what a child received, a coverage tool does not count what ran there, and a method that calls exit! ends its child, not the suite: the test raises Aw::ChildError.

In a Rails application

Add the gem to the test group of the Gemfile with require: false, as for Fix, and include Minitest::Fix in ActiveSupport::TestCase, or give RSpec::Fix to the examples in the helper of RSpec. A test of Rails runs in a transaction, rolled back when it ends, and that transaction belongs to the connection of its process: a child process gets a connection of its own. This is what a specification met there, in an application on Rails 8.1 and SQLite:

The specificationIn the process of the testWith fork: true
reads a row the test has createdfinds itdoes not find it, or counts none: a wrong value, without an error
writes, after the test has writtenwrites in the transactionwaits for the lock, five seconds here, then fails
writes, before the test has writtenwrites in the transactioncommits: the row is not rolled back, and stays after the test

So a value that reads or writes the database is checked in the process of the test, which is what the two gems do unless told otherwise. In a Rails application has the other way: tests of Fix, in fix, where each subject empties the database first.

From there

One specification in a test is where a project starts. The second is often a contract that several classes share, an interface and its implementations, which Patterns writes once and tests against each. From there a team may keep its suite as it is and write what is new as specifications, in fix: Coming from RSpec and Coming from minitest tell what each idiom becomes.

To take Fix out, remove the gems from the Gemfile, the tests that check a specification, and the fix directory. The suite is as it was.