minitest-fix
Assertions of minitest that test a value against a specification of Fix.
Minitest::Fix adds two assertions to the tests of minitest: assert_fix :Name, value passes when the value meets every expectation of a specification of fix, and its message gives the line of each one it does not; refute_fix is its opposite. It is the way to add a specification to a minitest suite, and to keep the suite. For RSpec, there is rspec-fix.
- Version
- 1.0.0
- Ruby
- 3.3 or later
- Dependencies
- fix
- License
- MIT
Install
Add gem "minitest-fix" to the Gemfile of the suite, then require "minitest/fix", which requires fix, and include Minitest::Fix in a test class, or in Minitest::Test itself for every test of the suite. The gem asks nothing of minitest but the assert and the refute of that class.
Usage
The name is the one of a specification registered by Fix :Name do … end, and KeyError is raised when there is none. In a suite, minitest/autorun runs the tests; the file below runs each by hand, so that it writes the message of the assertion alone, without the seed and the times of a report.
require "minitest"
require "minitest/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
class Duck
def walk = "Klop klop!"
def swim = "Swoosh..."
def speak = "Quaaack!"
end
class Robot
def walk = "Klop klop!"
def swim = "Glug glug..."
end
class RubberDuck
def swim = "Swoosh..."
def speak = "Squeak!"
end
class DuckTest < Minitest::Test
include Minitest::Fix
def test_duck = assert_fix(:Duck, Duck.new)
def test_robot = assert_fix(:Duck, Robot.new)
def test_rubber_duck = assert_fix(:Duck, RubberDuck.new)
end
# minitest/autorun would run the tests and write its report, with a seed and
# times; here each test is run by hand, so that the file writes the same lines
# every time.
DuckTest.new("test_duck").run.passed? # => true
DuckTest.new("test_robot").run.passed? # => true
puts DuckTest.new("test_rubber_duck").run.failure.message
ruby usage.rb Expected the value to fix Duck: usage.rb:6: Error: NoMethodError: undefined method 'walk' for an instance of RubberDuck usage.rb:14: Failure: expected "Squeak!" to eq "Quaaack!"
assert_fix passes when every expectation passes, a warning or an info included: the robot passes, though it swims its own way and does not speak, and minitest says nothing of it; Fix[:Duck].test { Robot.new } writes every line. Failed, it gives the lines of the failures and of the errors, each pointing at its expectation. refute_fix :Name, value passes when one expectation is not met, and each of the two counts as one assertion. They take a value, not a block; since the value is given once, a specification meant for an assertion is written without with.
The process of the test
The methods of the specification are sent to the value itself, in the process of the test, where its transaction, its mocks and its coverage tool are: a method that changes the value changes it for the contexts that follow, and for the rest of the test. assert_fix :Name, value, fork: true runs each context in a child process instead, as Fix does it: 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, since what lives outside is not copied: in a Rails application with transactional tests, a child does not see a row the test has created, and a row it writes is not rolled back; a mock does not see what a child received.