rspec-fix
A matcher of RSpec that tests a value against a specification of Fix.
RSpec::Fix adds the word fix to the examples of RSpec: expect(value).to fix(:Name) is met when the value meets every expectation of a specification of fix, and its message gives the line of each one it does not. It is the way to add a specification to an RSpec suite, and to keep the suite. For minitest, there is minitest-fix.
- Version
- 1.0.0
- Ruby
- 3.3 or later
- Dependencies
- fix, and rspec-expectations 3.13 or later
- License
- MIT
Install
Add gem "rspec-fix" to the Gemfile of the suite, then require "rspec/fix", which requires fix, and give the word to the examples: RSpec.configure { |config| config.include RSpec::Fix }. A group may include the module for itself instead.
Usage
fix(:Name) finds the specification registered by Fix :Name do … end, and raises KeyError when there is none. In a suite, an example reads it { is_expected.to fix(:Duck) }; the file below takes the words of an example for itself, so that it runs without a suite and writes the message of the matcher alone, without the seed and the times of a report.
require "rspec/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
# An example of RSpec has these words; here, outside a suite, the file takes them.
include RSpec::Matchers
include RSpec::Fix
expect(Duck.new).to fix(:Duck) # => true
expect(Robot.new).to fix(:Duck) # => true
begin
expect(RubberDuck.new).to fix(:Duck)
rescue RSpec::Expectations::ExpectationNotMetError => e
puts e.message
end
ruby usage.rb expected the value to fix Duck: usage.rb:5: Error: NoMethodError: undefined method 'walk' for an instance of RubberDuck usage.rb:13: Failure: expected "Squeak!" to eq "Quaaack!"
The matcher is met when every expectation passes, a warning or an info included: the robot passes, though it swims its own way and does not speak, and RSpec says nothing of it; Fix[:Duck].test { Robot.new } writes every line. Not met, it gives the lines of the failures and of the errors, each pointing at its expectation, and expect(value).not_to fix(:Name) is met when one expectation is not. It composes as the matchers of RSpec do: fix(:Duck).and fix(:Bird), all(fix(:Duck)) for the values of a collection, and as an argument of another matcher, include(pet: fix(:Duck)). It takes a value, not a block; since the value is given once, a specification meant for a matcher is written without with.
The process of the example
The methods of the specification are sent to the value itself, in the process of the example, where its transaction, its doubles and its coverage tool are: a method that changes the value changes it for the contexts that follow, and for the rest of the example. fix(:Name, 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 example 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 example has created, and a row it writes is not rolled back; a double does not see what a child received.