A long-lived project that still receives updates
Adds Minitest assertions to test for errors raised or not \ raised by Minitest itself. Most useful when testing other Minitest assertions.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Minitest::AssertErrors

Ruby - Gem Version - Minitest Style Guide

Coverage: 100%

Adds Minitest assertions to test for errors raised or not raised by Minitest itself. Most useful when testing other Minitest assertions or as a shortcut to other tests.

Added Methods

Currently adds the following methods:

Minitest::Assertions

  • assert_have_error()
  • assert_error_raised() (alias of assert_have_error())
  • assert_no_error()
  • :refute_error() (alias of assert_no_error())

Minitest::Expectations - for use with Minitest::Spec

  • _(actual).must_have_error(expected_msg)

  • _(actual).wont_have_error()


Installation

Add this line to your application's Gemfile:

gem 'minitest-assert_errors'

And then execute:

bundle

Or install it yourself as:

gem install minitest-assert_errors

Usage

Add the gem to your Gemfile or .gemspec file and then load the gem in your (test|spec)_helper.rb file as follows:

 # <snip...>

 require 'minitest/autorun'
 require 'minitest/assert_errors'

 # <snip...>

Adding the above to your spec_helper.rb file automatically adds the key helper methods to the Minitest::Assertions to test for Minitest errors raised or not raised within your tests.


Examples

assert_have_error(expected_msg, klass = Minitest::Assertion, &blk)

  -- also aliased as: assert_error_raised()

Assertion method to test for an error raised by Minitest

  assert_have_error('error message') do
    assert(false, 'error message')
  end

  # or

  _{
    assert(false, 'error message')
  }.must_have_error('error message')

Produces a longer error message, combining the given error message with the default error message, when something is wrong.

NOTE! The expected error message can be a String or Regexp.

  assert_have_error(/error message.+Actual:\s+\"b\"/m) do
    assert_equal('a','b', 'error message')
  end

  # or

  _{
    assert_equal('a','b', 'error message')
  }.must_have_error(/error message.+Actual:\s+\"b\"/m)

assert_no_error(&blk)

  -- also aliased as: refute_error() or refute_error_raised() or assert_no_error_raised()

Assertion method to test for no error being raised by Minitest test.

  assert_no_error() do
    assert(true, 'error message')
  end

  # or

  _{ assert(true) }.wont_have_error

Produces a longer error message, combining the given error message with the default error message, when something is wrong.

NOTE! The expected error message can be a String or Regexp.

 assert_no_error { assert_equal('a', :a, 'error message') }

   #=> "error message.\nExpected: \"a\"\n  Actual: :a"

 proc {
   assert_equal('a', :a, 'error message')
 }.wont_have_error

   #=> "error message.\nExpected: \"a\"\n  Actual: :a"

Development

After checking out the repo, run bundle install to install dependencies. Then, run bundle exec rake spec to run the tests.

To install this gem onto your local machine, run bundle exec rake install.

To release a new version:

  1. update the version number in version.rb
  2. run bundle exec rake release, which will create a git tag for the version
  3. push git commits and tags
  4. push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub.

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Copyright

Copyright (c) 2015 - 2024 Kematzy

License

The gem is available as open source under the terms of the MIT License.