module Minitest::Assertions
Public Instance Methods
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.
# File lib/minitest-bonus-assertions.rb, line 39 def assert_between(*args) lo, hi, exp, msg = if args.first.kind_of?(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
Fails unless obj
is literally false
.
# 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
Fails unless obj
has all of the keys
listed.
# File lib/minitest-bonus-assertions.rb, line 53 def assert_has_keys obj, keys, msg = nil keys = [ keys ] unless keys.kind_of?(Array) msg = message(msg) { "Expected #{mu_pp(obj)} to include all keys #{mu_pp(keys)}" } keys.all? { |key| assert obj.key?(key), msg } end
Fails if obj
has any of the keys listed.
# File lib/minitest-bonus-assertions.rb, line 63 def assert_missing_keys obj, keys, msg = nil keys = [ keys ] unless keys.kind_of?(Array) msg = message(msg) { "Expected #{mu_pp(obj)} not to include any of these keys #{mu_pp(keys)}" } keys.none? { |key| refute obj.key?(key), msg } end
Fails unless the block raises exp
with the message exp_msg
. Returns the exception matched so you can check other attributes.
# File lib/minitest-bonus-assertions.rb, line 76 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
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]
# File lib/minitest-bonus-assertions.rb, line 122 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
Fails unless the set from actual
matches the set from exp
.
# File lib/minitest-bonus-assertions.rb, line 90 def assert_set_equal expected, actual, msg = nil require 'set' 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
Fails unless obj
is literally true
.
# 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
Fails unless the set from actual
differs from the set from exp
.
# File lib/minitest-bonus-assertions.rb, line 101 def refute_set_equal expected, actual, msg = nil require 'set' 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