Module: Minitest::Assertions

Defined in:
lib/minitest/bonus_assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert_between(*args) ⇒ Object

Fails unless exp is between lo and hi, or is in range. This test is exclusive of the boundaries. That is:

assert_between 1, 10, 1

will return false, but:

assert_between 0.99, 10.1, 1

will return true.

:call-seq:

assert_between lo, hi, exp, msg = nil
assert_between range, exp, msg = nil


39
40
41
42
43
44
45
46
47
48
# File 'lib/minitest/bonus_assertions.rb', line 39

def assert_between(*args)
  lo, hi, exp, msg = if args.first.is_a?(Range)
    [args.first.begin, args.first.end, args[1], args[2]]
  else
    args[0..3]
  end
  lo, hi = hi, lo if lo > hi
  msg = message(msg) { "Expected #{mu_pp(exp)} to be between #{mu_pp(lo)} and #{mu_pp(hi)}" }
  assert (lo < exp && exp < hi), msg
end

#assert_false(obj, msg = nil) ⇒ Object

Fails unless obj is literally false.



10
11
12
13
# File 'lib/minitest/bonus_assertions.rb', line 10

def assert_false obj, msg = nil
  msg = message(msg) { "<false> expected but was #{mu_pp(obj)}" }
  assert obj == false, msg
end

#assert_has_keys(obj, keys, msg = nil) ⇒ Object Also known as: refute_missing_keys

Fails unless obj has all of the keys listed.



53
54
55
56
57
# File 'lib/minitest/bonus_assertions.rb', line 53

def assert_has_keys obj, keys, msg = nil
  keys = [keys] unless keys.is_a?(Array)
  msg = message(msg) { "Expected #{mu_pp(obj)} to include all keys #{mu_pp(keys)}" }
  keys.all? { |key| assert obj.key?(key), msg }
end

#assert_missing_keys(obj, keys, msg = nil) ⇒ Object Also known as: refute_has_keys

Fails if obj has any of the keys listed.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/minitest/bonus_assertions.rb', line 63

def assert_missing_keys obj, keys, msg = nil
  keys = [keys] unless keys.is_a?(Array)
  msg = message(msg) {
    "Expected #{mu_pp(obj)} not to include any of these keys #{mu_pp(keys)}"
  }

  keys.each do |key|
    refute obj.key?(key), msg
  end

  true
end

#assert_raises_with_message(exp, exp_msg, msg = nil) ⇒ Object

Fails unless the block raises exp with the message exp_msg. Returns the exception matched so you can check other attributes.



81
82
83
84
85
86
87
88
89
90
# File 'lib/minitest/bonus_assertions.rb', line 81

def assert_raises_with_message exp, exp_msg, msg = nil
  msg = message(msg) { "#{mu_pp(exp)} exception expected with message #{mu_pp(exp_msg)}" }

  exception = assert_raises exp do
    yield
  end

  assert_equal exp_msg, exception.message, msg
  exception
end

#assert_result_equal(expr, actual, msg = nil) ⇒ Object

Fails unless actual is the same value as the result of evaluating expr in the context of the test case through instance_eval (when expr is a String) or instance_exec (when expr is a callable).

If expr results in nil, this test delegates to #assert_nil, otherwise it delegates to #assert_equal.

This assertion exists because Minitest 6 is changing #assert_equal so that it fails when comparing against nil.

assert_result_equal -> { model.department }, response[:department]
assert_result_equal 'model.department', response[:department]


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/minitest/bonus_assertions.rb', line 125

def assert_result_equal expr, actual, msg = nil
  result = if expr.respond_to?(:call)
    instance_exec(&expr)
  else
    instance_eval(expr)
  end

  if result.nil?
    msg = message(msg) {
      "nil expected (expr #{mu_pp(expr)}) but was #{mu_pp(actual)}"
    }
    assert_nil actual, msg
  else
    msg = message(msg) {
      "#{mu_pp(result)} expected (expr #{mu_pp(expr)}) but was #{mu_pp(actual)}"
    }
    assert_equal result, actual, msg
  end
end

#assert_set_equal(expected, actual, msg = nil) ⇒ Object

Fails unless the set from actual matches the set from exp.



95
96
97
98
99
100
# File 'lib/minitest/bonus_assertions.rb', line 95

def assert_set_equal expected, actual, msg = nil
  msg = message(msg) {
    "Expected #{mu_pp(actual)} to be set equivalent to #{mu_pp(expected)}"
  }
  assert_equal Set.new(expected), Set.new(actual), msg
end

#assert_true(obj, msg = nil) ⇒ Object

Fails unless obj is literally true.



18
19
20
21
# File 'lib/minitest/bonus_assertions.rb', line 18

def assert_true obj, msg = nil
  msg = message(msg) { "<true> expected but was #{mu_pp(obj)}" }
  assert obj == true, msg
end

#refute_set_equal(expected, actual, msg = nil) ⇒ Object

Fails unless the set from actual differs from the set from exp.



105
106
107
108
109
110
# File 'lib/minitest/bonus_assertions.rb', line 105

def refute_set_equal expected, actual, msg = nil
  msg = message(msg) {
    "Expected #{mu_pp(actual)} not to be set equivalent to #{mu_pp(expected)}"
  }
  refute_equal Set.new(expected), Set.new(actual), msg
end