Module: Minitest::AssertionTests

Defined in:
lib/minitest/assertion_tests.rb

Overview

Use Minitest::AssertionTests to test assertions for Minitest.

describe Minitest::BonusAssertions do
  include Minitest::AssertionTests

  it 'be triggered for false or nil' do
    assert_expected_assertions 2 do
      assert_assertion_triggered '<true> expected but was false.' do
        tc.assert_true false
      end

      assert_assertion_triggered '<true> expected but was nil.' do
        tc.assert_true nil
      end
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#specObject (readonly)

The test spec to use for the expectation under test.



23
24
25
# File 'lib/minitest/assertion_tests.rb', line 23

def spec
  @spec
end

#tcObject (readonly)

The test case to use for the assertion under test.



21
22
23
# File 'lib/minitest/assertion_tests.rb', line 21

def tc
  @tc
end

Instance Method Details

#assert_assertion_triggered(expected, klass = Minitest::Assertion) ⇒ Object

Specify that the assertion was expected to fail with the resulting message.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/minitest/assertion_tests.rb', line 45

def assert_assertion_triggered expected, klass = Minitest::Assertion
  e = assert_raises(klass) do
    yield
  end

  msg = e.message.sub(/(---Backtrace---).*/m, '\1')
  msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)')
  msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform

  assert_equal expected, msg
end

#assert_expected_assertions(expected = 1) ⇒ Object

Specify the number of assertions that should be called during the test under this block. Most tests should be wrapped in this assertion.



36
37
38
39
40
41
42
# File 'lib/minitest/assertion_tests.rb', line 36

def assert_expected_assertions expected = 1
  yield
ensure
  actual = tc.assertions + spec.assertions
  assert_equal expected, actual, "expected #{expected} assertions to be " +
    "fired during the test, not #{actual}"
end

#setupObject

:nodoc:



25
26
27
28
29
30
31
32
# File 'lib/minitest/assertion_tests.rb', line 25

def setup # :nodoc:
  super

  Minitest::Test.reset

  @tc = Minitest::Test.new 'fake test case'
  @spec = Minitest::Spec.new 'fake test spec'
end