Cheat Sheet: Minitest

WIP — check back soon!


Writing a unit-style test:

class TestArrays < Minitest::Test
  def test_array_includes_symbols
    assert_includes [:a, :b], :a
  end
end

Writing a spec-style test:

describe "namespace support" do
  def to_js(string)
    _(Ruby2JS.convert(string, eslevel: 2017, filters: []).to_s)
  end

  describe "open modules" do
    it "should extend modules" do
      to_js( 'module M; def f(); end; end;' +
             'module M; def g(); end; end').
      must_equal('const M = {f() {}}; ' +
        'M.g = function() {}');
    end
  end
end

The nice thing is, you even can leverage the expressive DSL of spec tests and expectations even in a unit test class.

###

It’s common to subclass tests from a common superclass you can augment with your own helpers and other setup code:

require "minitest_helper"

class TestAllTheThings < MyUnitTest
  # test cases here
end
# minitest_helper.rb

class MyUnitTest < Minitest::Test

end

Checking Your Data

Assertion / Expectation Description Example
assert_empty
must_be_empty
refute_empty
wont_be_empty

Validates if the collection is empty

Fails if the collection is empty

assert_empty []
expect([]).must_be_empty
refute_empty [:hello]
expect([:hello]).wont_be_empty
assert_equal
must_equal
refute_equal
wont_equal

Validates if two values match according to the equality (==) operator

Fails if two values match according to the equality (==) operator

assert_equal 42, i
expect(i).must_equal 42
refute_equal 666, i
expect(i).wont_equal 666
assert_includes
must_include
refute_includes
wont_inlude

Validates if the value can found within the collection

Fails if the value can be found within the collection

assert_includes [31, 42, 53], 42
expect([31, 42, 53]).must_include 42
refute_includes [31, 42, 53], 666
expect([31, 42, 53]).wont_include 666
assert_instance_of
must_be_instance_of
refute_instance_of
wont_be_instance_of

Validates if the test object is exactly an instance of the expected class

Fails if the test object is exactly an instance of the expected class

assert_instance_of Set, coll
expect(coll).must_be_instance_of Set
refute_instance_of Float, flt
expect(flt).wont_be_instance_of Float
assert_kind_of
must_be_kind_of
refute_kind_of
wont_be_kind_of

Validates if the test object is an instance of the expected class (or subclass)

Fails if the test object is an instance of the expected class (or subclass)

assert_kind_of Set, coll
expect(coll).must_be_kind_of Set
refute_kind_of Float, flt
expect(flt).wont_be_kind_of Float
assert_match
must_match
refute_match
wont_match

Validates if the value matches a regular expression (according to the =~ operator)

Fails if the value matches a regular expression (according to the =~ operator)

assert_match /^[0-9]+/, str
expect(str).must_match /^[0-9]+/
refute_match /^[0-9]+/, str
expect(str).wont_match /^[0-9]+/
assert_nil
must_be_nil
refute_nil
wont_be_nil

Validates if the value is nil?

Fails if the value is nil?

assert_nil hsh[:nothing]
expect(hsh[:nothing]).must_be_nil
refute_nil hsh[:something]
expect(hsh[:something]).wont_be_nil
assert_raises
must_raise

Validates if the code executed within the block raises the provided exception

assert_raises(CustomError) { run_code_that_raises_error }
expect { run_code_that_raises_error }.must_raise CustomError
Skip to content