Minitest::MatchersVaccine
Adds matcher support to minitest without all the other RSpec-style expectation infections.
Using matchers with RSpec-style expectations requires that we infect the objects that we are testing with new methods. Matcher implementations are typically overkill, but there are a lot of good testing libraries that still insist on standardizing on matchers. These matchers still have some value, and this gem tries to extract that value with straight-forward assertions that adhere to the matcher spec.
Why not use minitest-matchers? This gem is actually heavily inspired by and based upon the assertions in minitest-matchers; however, everything else that minitest-matchers brings to the table is unnecessary unless you're bent on a true RSpec-style syntax.
Installation
Add this line to your application's Gemfile:
gem "minitest-matchers_vaccine"
And then execute:
$ bundle
Or install it yourself as:
$ gem install minitest-matchers_vaccine
Usage
Includes both assert_must
and assert_wont
assertions, but also includes
must
and wont
facilitator assertions that automatically default to using
the current subject
method (aka "let variable") or @subject
instance
variable.
NOTE: This gem does not allow matchers to be used with an expectation syntax. Let's avoid infecting the objects we're testing.
Minitest::Test
class UserTest < Minitest::Test
def setup
@subject = User.new
end
def test_fields_and_associations
must have_db_column :name
must belong_to :account
assert_must have_many(:widgets), @subject
end
def test_validations
must have_valid(:email).when("a@a.com", "foo@bar.com", "dave@abc.io")
wont have_valid(:email).when(nil, "foo", "foo@bar", "@bar.com")
end
# Works with matchers in other libs
def test_stripping
assert_must strip_attribute(:name), User.new
end
end
Minitest::Spec
describe User do
subject { User.new }
# Works with shoulda-matchers
it "should have fields and associations" do
must have_db_column :name
must belong_to :account
must have_many :widgets
end
# Works with valid_attribute
it "should validate" do
must have_valid(:email).when("a@a.com", "foo@bar.com", "dave@abc.io")
wont have_valid(:email).when(nil, "foo", "foo@bar", "@bar.com")
end
# Works with matchers in other libs
it "should strip attributes" do
must strip_attribute :name
end
end
Contributing
- Fork it ( https://github.com/rmm5t/minitest-matchers_vaccine/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Credits
The idea was originally inspired by the matcher assertions implementation in minitest-matchers.