Fix::Expect
Provides the
expect
syntax.
Installation
Add this line to your application's Gemfile:
gem "fix-expect"
And then execute:
$ bundle
Or install it yourself as:
$ gem install fix-expect
Usage
The expect
method
Fix::Expect lets you express expected outcomes on an object, thanks to Spectus's MUST
and MUST_NOT
requirement levels.
An absolute requirement example:
Fix do
it { expect(-42.abs).to equal 42 }
end
(irb):2: Success: expected to equal 42.
An absolute prohibition example:
Fix do
it { expect(-42).not_to equal 42 }
end
(irb):5: Success: expected -42 not to equal 42.
Thus, rather than inferring the actual value (which is 41.next
) from the context
and calculating its value (which is 42
),
rather than letting Fix's default behavior define and compute 41.next
as the actual value to challenge,
the expect
method short circuit it with its argument.
These 2 examples are equivalent:
Fix 41 do
on :next do
it { MUST equal 42 }
it { expect(41.next).to equal 42 }
end
it { MUST equal 41 }
it { expect(41.next).to equal 42 }
end
(irb):9: Success: expected to equal 42. (irb):10: Success: expected to equal 42. (irb):13: Success: expected to equal 41. (irb):14: Success: expected to equal 42.
The block syntax is also allowed:
Fix do
it { expect { -42.abs }.to equal 42 }
end
(irb):2: Success: expected to equal 42.
Fix do
it { expect { 4 / 0 }.to raise_exception ZeroDivisionError }
end
(irb):5: Success: divided by 0.
The is_expected
method
For convenience, an is_expected
method is provided,
as an alias of Spectus's MUST:
Fix 41 do
on :next do
it { is_expected.to equal 42 }
end
end
(irb):3: Success: expected to equal 42.
Code Isolation
When executing expectations, side-effects may occur. Because they may or may not be desired, each requirement level has 2 versions:
- if it is performed with
do
, a test is performed without isolation; - if it is performed with
do!
, a test is performed in isolation.
Example of test without isolation:
greeting = "Hello, world!"
Fix do
it "tests without isolation" do
expect { greeting.gsub!("world", "Alice") }.to equal "Hello, Alice!"
end
end
greeting # => "Hello, Alice!"
Example of test in isolation:
greeting = "Hello, world!"
Fix do
it "tests with isolation" do
expect { greeting.gsub!("world", "Alice") }.to! equal "Hello, Alice!"
end
end
greeting # => "Hello, world!"
Contact
- Source code: https://github.com/fixrb/fix-expect
Versioning
Fix::Expect follows Semantic Versioning 2.0.
License
The gem is available as open source under the terms of the MIT License.