Expect syntax for Minitest. Made out of love for both rspec2's expect syntax and Minitest's lightweight feel.
In your Gemfile:
gem 'minitest-spec-expect'
or from the command line:
$ gem install minitest-spec-expect
Then require it after your runner:
require 'minitest/autorun'
require 'minitest/spec/expect'
minitest-spec-expect works in the following ruby versions and implementations:
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1.0
- JRuby
Wrap the object under test in an expect()
object. Then call a Minitest expectation on it,
substituting must
and wont
with to
and to_not
.
For example:
describe Integer do
describe '#+' do
it 'adds numbers correctly' do
expect(1 + 1).to_equal 2
end
it 'does not add numbers incorrectly' do
expect(1 + 1).to_not_equal 3
end
end
end
That's equivalent to:
describe Integer do
describe '#+' do
it 'adds numbers correctly' do
(1 + 1).must_equal 2
end
it 'does not add numbers incorrectly' do
(1 + 1).wont_equal 3
end
end
end
Please see the Minitest expectation
docs for details on corresponding must_*
and wont_*
methods.
expect(1).to_be :<, 2
expect(1).to_not_be :>, 2
expect(Math::PI).to_be_close_to 22.0/7.0, 0.01
expect(Math::PI).to_be_within_delta 22.0/7.0, 0.01
expect(Math::PI).to_not_be_close_to 22.0/6.0, 0.01
expect(Math::PI).to_not_be_within_delta 22.0/6.0, 0.01
expect([]).to_be_empty
expect({awwyeah: :minitest}).to_not_be_empty
expect([]).to_be_instance_of Array
expect([]).to_not_be_instance_of Hash
expect([]).to_be_kind_of Enumerable
expect([]).to_not_be_kind_of NilClass
expect(nil).to_be_nil
expect(:something).to_not_be_nil
expect(obj = Object.new).to_be_same_as obj
an_object = Object.new
expect(Object.new).to_not_be_same_as an_object
expect { '' }.to_be_silent
expect(->{''}).to_be_silent
expect(4).to_be_within_epsilon 3.5, 0.15
expect(4).to_not_be_within_epsilon 3.5, 0.11
expect('4d3d3d3d').to_equal '4d3d3d3d'
expect('4d3d3d3d').to_not_equal 'tayne'
expect(['tim', 'eric']).to_include 'eric'
expect(['tim', 'eric']).to_not_include 'brule'
expect('fartfartfartfart').to_match /(fart)*/
expect('bart').to_not_match /fart/
expect { puts 'barf' }.to_output "barf\n"
expect(->{ puts 'barf' }).to_output "barf\n"
expect { raise RuntimeError }.to_raise RuntimeError
expect(->{ raise RuntimeError }).to_raise RuntimeError
expect(Object.new).to_respond_to :must_respond_to
expect(Object.new).to_not_respond_to :fart_factory
expect { throw StandardError }.to_throw StandardError
expect(->{ throw StandardError }).to_throw StandardError
- Fork the repo.
- Create a branch.
- Code.
- Ensure the specs are green (
$ rake
) - Open a pull request.
The MIT License (MIT)
Copyright (c) 2013 Dave Jachimiak
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.