Class Index [+]

Quicksearch

Test::Unit::Assertions

Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is included in Test::Unit::TestCase.

To include it in your own code and use its functionality, you simply need to rescue Test::Unit::AssertionFailedError. Additionally you may override add_assertion to get notified whenever an assertion is made.

Notes:

Example Custom Assertion

  def deny(boolean, message = nil)
    message = build_message message, '<?> is not false or nil.', boolean
    assert_block message do
      not boolean
    end
  end

Constants

UncaughtThrow

Public Class Methods

use_pp=(value) click to toggle source

Select whether or not to use the pretty-printer. If this option is set to false before any assertions are made, pp.rb will not be required.

      # File lib/test/unit/assertions.rb, line 1380
1380:       def self.use_pp=(value)
1381:         AssertionMessage.use_pp = value
1382:       end

Public Instance Methods

assert(boolean, message=nil) click to toggle source

Asserts that boolean is not false or nil.

Example:

  assert [1, 2].include?(5)
    # File lib/test/unit/assertions.rb, line 63
63:       def assert(boolean, message=nil)
64:         _wrap_assertion do
65:           case message
66:           when nil, String, Proc
67:           else
68:             error_message = "assertion message must be String or Proc: "
69:             error_message << "<#{message.inspect}>(<#{message.class}>)"
70:             raise ArgumentError, error_message, filter_backtrace(caller)
71:           end
72:           assert_block("assert should not be called with a block.") do
73:             !block_given?
74:           end
75:           assert_block(build_message(message, "<?> is not true.", boolean)) do
76:             boolean
77:           end
78:         end
79:       end
assert_alias_method(object, alias_name, original_name, message=nil) click to toggle source

Passes if object#alias_name is an alias method of object#original_name.

Example:

  assert_alias_method([], :length, :size)  # -> pass
  assert_alias_method([], :size, :length)  # -> pass
  assert_alias_method([], :each, :size)    # -> fail
      # File lib/test/unit/assertions.rb, line 1175
1175:       def assert_alias_method(object, alias_name, original_name, message=nil)
1176:         _wrap_assertion do
1177:           find_method_failure_message = Proc.new do |method_name|
1178:             build_message(message,
1179:                           "<?>.? doesn't exist\n" +
1180:                           "(Class: <?>)",
1181:                           object,
1182:                           AssertionMessage.literal(method_name),
1183:                           object.class)
1184:           end
1185: 
1186:           alias_method = original_method = nil
1187:           assert_block(find_method_failure_message.call(alias_name)) do
1188:             begin
1189:               alias_method = object.method(alias_name)
1190:               true
1191:             rescue NameError
1192:               false
1193:             end
1194:           end
1195:           assert_block(find_method_failure_message.call(original_name)) do
1196:             begin
1197:               original_method = object.method(original_name)
1198:               true
1199:             rescue NameError
1200:               false
1201:             end
1202:           end
1203: 
1204:           full_message = build_message(message,
1205:                                        "<?> is alias of\n" +
1206:                                        "<?> expected",
1207:                                        alias_method,
1208:                                        original_method)
1209:           assert_block(full_message) do
1210:             alias_method == original_method
1211:           end
1212:         end
1213:       end
assert_block(message="assert_block failed.") click to toggle source

The assertion upon which all other assertions are based. Passes if the block yields true.

Example:

  assert_block "Couldn't do the thing" do
    do_the_thing
  end
    # File lib/test/unit/assertions.rb, line 48
48:       def assert_block(message="assert_block failed.") # :yields:
49:         _wrap_assertion do
50:           if (! yield)
51:             raise AssertionFailedError.new(message.to_s)
52:           end
53:         end
54:       end
assert_boolean(actual, message=nil) click to toggle source

Passes if actual is a boolean value.

Example:

  assert_boolean(true) # -> pass
  assert_boolean(nil)  # -> fail
     # File lib/test/unit/assertions.rb, line 951
951:       def assert_boolean(actual, message=nil)
952:         _wrap_assertion do
953:           assert_block(build_message(message,
954:                                      "<true> or <false> expected but was\n<?>",
955:                                      actual)) do
956:             [true, false].include?(actual)
957:           end
958:         end
959:       end
assert_compare(expected, operator, actual, message=nil) click to toggle source

Passes if expression “expected operator actual“ is true.

Example:

  assert_compare(1, "<", 10)  # -> pass
  assert_compare(1, ">=", 10) # -> fail
      # File lib/test/unit/assertions.rb, line 1000
1000:       def assert_compare(expected, operator, actual, message=nil)
1001:         _wrap_assertion do
1002:           assert_send([["<", "<=", ">", ">="], :include?, operator.to_s])
1003:           case operator.to_s
1004:           when "<"
1005:             operator_description = "less than"
1006:           when "<="
1007:             operator_description = "less than or equal to"
1008:           when ">"
1009:             operator_description = "greater than"
1010:           when ">="
1011:             operator_description = "greater than or equal to"
1012:           end
1013:           template = <?> #{operator} <?> should be true<?> expected #{operator_description}<?>.
1014:           full_message = build_message(message, template,
1015:                                        expected, actual,
1016:                                        expected, actual)
1017:           assert_block(full_message) do
1018:             expected.send(operator, actual)
1019:           end
1020:         end
1021:       end
assert_const_defined(object, constant_name, message=nil) click to toggle source

Passes if object.const_defined?(constant_name)

Example:

  assert_const_defined(Test, :Unit)          # -> pass
  assert_const_defined(Object, :Nonexistent) # -> fail
      # File lib/test/unit/assertions.rb, line 1095
1095:       def assert_const_defined(object, constant_name, message=nil)
1096:         _wrap_assertion do
1097:           full_message = build_message(message,
1098:                                        "<?>.const_defined\\?(<?>) expected.",
1099:                                        object, constant_name)
1100:           assert_block(full_message) do
1101:             object.const_defined?(constant_name)
1102:           end
1103:         end
1104:       end
assert_empty(object, message=nil) click to toggle source

Passes if object is empty.

Example:

  assert_empty("")                       # -> pass
  assert_empty([])                       # -> pass
  assert_empty({})                       # -> pass
  assert_empty(" ")                      # -> fail
  assert_empty([nil])                    # -> fail
  assert_empty({1 => 2})                 # -> fail
      # File lib/test/unit/assertions.rb, line 1305
1305:       def assert_empty(object, message=nil)
1306:         _wrap_assertion do
1307:           assert_respond_to(object, :empty?,
1308:                             "The object must respond to :empty?.")
1309:           full_message = build_message(message,
1310:                                        "<?> expected to be empty.",
1311:                                        object)
1312:           assert_block(full_message) do
1313:             object.empty?
1314:           end
1315:         end
1316:       end
assert_equal(expected, actual, message=nil) click to toggle source

Passes if expected == +actual.

Note that the ordering of arguments is important, since a helpful error message is generated when this one fails that tells you the values of expected and actual.

Example:

  assert_equal 'MY STRING', 'my string'.upcase
     # File lib/test/unit/assertions.rb, line 92
 92:       def assert_equal(expected, actual, message=nil)
 93:         diff = AssertionMessage.delayed_diff(expected, actual)
 94:         if expected.respond_to?(:encoding) and
 95:             actual.respond_to?(:encoding) and
 96:             expected.encoding != actual.encoding
 97:           format = <?>(?) expected but was<?>(?).?
 98:           full_message = build_message(message, format,
 99:                                        expected, expected.encoding.name,
100:                                        actual, actual.encoding.name,
101:                                        diff)
102:         else
103:           full_message = build_message(message, <?> expected but was<?>.?, expected, actual, diff)
104:         end
105:         begin
106:           assert_block(full_message) { expected == actual }
107:         rescue AssertionFailedError => failure
108:           failure.expected = expected
109:           failure.actual = actual
110:           failure.inspected_expected = AssertionMessage.convert(expected)
111:           failure.inspected_actual = AssertionMessage.convert(actual)
112:           failure.user_message = message
113:           raise
114:         end
115:       end
assert_fail_assertion(message=nil) click to toggle source

Passes if assertion is failed in block.

Example:

  assert_fail_assertion {assert_equal("A", "B")}  # -> pass
  assert_fail_assertion {assert_equal("A", "A")}  # -> fail
      # File lib/test/unit/assertions.rb, line 1033
1033:       def assert_fail_assertion(message=nil)
1034:         _wrap_assertion do
1035:           full_message = build_message(message,
1036:                                        "Failed assertion was expected.")
1037:           assert_block(full_message) do
1038:             begin
1039:               yield
1040:               false
1041:             rescue AssertionFailedError
1042:               true
1043:             end
1044:           end
1045:         end
1046:       end
assert_false(actual, message=nil) click to toggle source

Passes if actual is false.

Example:

  assert_false(false)  # -> pass
  assert_false(nil)    # -> fail
     # File lib/test/unit/assertions.rb, line 983
983:       def assert_false(actual, message=nil)
984:         _wrap_assertion do
985:           assert_block(build_message(message,
986:                                      "<false> expected but was\n<?>",
987:                                      actual)) do
988:             actual == false
989:           end
990:         end
991:       end
assert_in_delta(expected_float, actual_float, delta=0.001, message="") click to toggle source

Passes if expected_float and actual_float are equal within delta tolerance.

Example:

  assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
     # File lib/test/unit/assertions.rb, line 613
613:       def assert_in_delta(expected_float, actual_float, delta=0.001, message="")
614:         _wrap_assertion do
615:           _assert_in_delta_validate_arguments(expected_float,
616:                                               actual_float,
617:                                               delta)
618:           full_message = _assert_in_delta_message(expected_float,
619:                                                   actual_float,
620:                                                   delta,
621:                                                   message)
622:           assert_block(full_message) do
623:             (expected_float.to_f - actual_float.to_f).abs <= delta.to_f
624:           end
625:         end
626:       end
assert_in_epsilon(expected_float, actual_float, epsilon=0.001, message="") click to toggle source

Passes if expected_float and actual_float are equal within epsilon relative error of expected_float.

Example:

  assert_in_epsilon(10000.0, 9900.0, 0.1) # -> pass
  assert_in_epsilon(10000.0, 9899.0, 0.1) # -> fail
     # File lib/test/unit/assertions.rb, line 737
737:       def assert_in_epsilon(expected_float, actual_float, epsilon=0.001,
738:                             message="")
739:         _wrap_assertion do
740:           _assert_in_epsilon_validate_arguments(expected_float,
741:                                                 actual_float,
742:                                                 epsilon)
743:           full_message = _assert_in_epsilon_message(expected_float,
744:                                                     actual_float,
745:                                                     epsilon,
746:                                                     message)
747:           assert_block(full_message) do
748:             normalized_expected_float = expected_float.to_f
749:             delta = normalized_expected_float * epsilon.to_f
750:             (normalized_expected_float - actual_float.to_f).abs <= delta
751:           end
752:         end
753:       end
assert_include(collection, object, message=nil) click to toggle source

Passes if collection includes object.

Example:

  assert_include([1, 10], 1)            # -> pass
  assert_include(1..10, 5)              # -> pass
  assert_include([1, 10], 5)            # -> fail
  assert_include(1..10, 20)             # -> fail
      # File lib/test/unit/assertions.rb, line 1259
1259:       def assert_include(collection, object, message=nil)
1260:         _wrap_assertion do
1261:           assert_respond_to(collection, :include?,
1262:                             "The collection must respond to :include?.")
1263:           full_message = build_message(message,
1264:                                        "<?> expected to include\n<?>.",
1265:                                        collection,
1266:                                        object)
1267:           assert_block(full_message) do
1268:             collection.include?(object)
1269:           end
1270:         end
1271:       end
assert_instance_of(klass, object, message="") click to toggle source

Passes if object.instance_of?(klass). When klass is an array of classes, it passes if any class satisfies +object.instance_of?(class).

Example:

  assert_instance_of(String, 'foo')            # -> pass
  assert_instance_of([Fixnum, NilClass], 100)  # -> pass
  assert_instance_of([Numeric, NilClass], 100) # -> fail
     # File lib/test/unit/assertions.rb, line 200
200:       def assert_instance_of(klass, object, message="")
201:         _wrap_assertion do
202:           klasses = nil
203:           klasses = klass if klass.is_a?(Array)
204:           assert_block("The first parameter to assert_instance_of should be " +
205:                        "a Class or an Array of Class.") do
206:             if klasses
207:               klasses.all? {|k| k.is_a?(Class)}
208:             else
209:               klass.is_a?(Class)
210:             end
211:           end
212:           klass_message = AssertionMessage.maybe_container(klass) do |value|
213:             "<#{value}>"
214:           end
215:           full_message = build_message(message, <?> expected to be an instance of? but was<?>., object, klass_message, object.class)
216:           assert_block(full_message) do
217:             if klasses
218:               klasses.any? {|k| object.instance_of?(k)}
219:             else
220:               object.instance_of?(klass)
221:             end
222:           end
223:         end
224:       end
assert_kind_of(klass, object, message="") click to toggle source

Passes if object.kind_of?(klass). When klass is an array of classes or modules, it passes if any class or module satisfies +object.kind_of?(class_or_module).

Example:

  assert_kind_of(Object, 'foo')                # -> pass
  assert_kind_of([Fixnum, NilClass], 100)      # -> pass
  assert_kind_of([Fixnum, NilClass], "string") # -> fail
     # File lib/test/unit/assertions.rb, line 255
255:       def assert_kind_of(klass, object, message="")
256:         _wrap_assertion do
257:           klasses = nil
258:           klasses = klass if klass.is_a?(Array)
259:           assert_block("The first parameter to assert_kind_of should be " +
260:                        "a kind_of Module or an Array of a kind_of Module.") do
261:             if klasses
262:               klasses.all? {|k| k.kind_of?(Module)}
263:             else
264:               klass.kind_of?(Module)
265:             end
266:           end
267:           klass_message = AssertionMessage.maybe_container(klass) do |value|
268:             "<#{value}>"
269:           end
270:           full_message = build_message(message,
271:                                        "<?> expected to be kind_of\\?\n" +
272:                                        "? but was\n" +
273:                                        "<?>.",
274:                                        object,
275:                                        klass_message,
276:                                        object.class)
277:           assert_block(full_message) do
278:             if klasses
279:               klasses.any? {|k| object.kind_of?(k)}
280:             else
281:               object.kind_of?(klass)
282:             end
283:           end
284:         end
285:       end
assert_match(pattern, string, message="") click to toggle source

Passes if string =~ pattern.

Example:

  assert_match(/\d+/, 'five, 6, seven')
     # File lib/test/unit/assertions.rb, line 343
343:       def assert_match(pattern, string, message="")
344:         _wrap_assertion do
345:           pattern = case(pattern)
346:             when String
347:               Regexp.new(Regexp.escape(pattern))
348:             else
349:               pattern
350:           end
351:           full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern)
352:           assert_block(full_message) { string =~ pattern }
353:         end
354:       end
assert_nil(object, message="") click to toggle source

Passes if object is nil.

Example:

  assert_nil [1, 2].uniq!
     # File lib/test/unit/assertions.rb, line 237
237:       def assert_nil(object, message="")
238:         full_message = build_message(message, <?> expected to be nil., object)
239:         assert_block(full_message) { object.nil? }
240:       end
assert_no_match(regexp, string, message="") click to toggle source

Deprecated. Use # instead.

Passes if regexp !~ string

Example:

  assert_no_match(/two/, 'one 2 three')   # -> pass
  assert_no_match(/three/, 'one 2 three') # -> fail
     # File lib/test/unit/assertions.rb, line 512
512:       def assert_no_match(regexp, string, message="")
513:         _wrap_assertion do
514:           assert_instance_of(Regexp, regexp,
515:                              "The first argument to assert_no_match " +
516:                              "should be a Regexp.")
517:           assert_not_match(regexp, string, message)
518:         end
519:       end
assert_not_const_defined(object, constant_name, message=nil) click to toggle source

Passes if !object.const_defined?(constant_name)

Example:

  assert_not_const_defined(Object, :Nonexistent) # -> pass
  assert_not_const_defined(Test, :Unit)          # -> fail
      # File lib/test/unit/assertions.rb, line 1112
1112:       def assert_not_const_defined(object, constant_name, message=nil)
1113:         _wrap_assertion do
1114:           full_message = build_message(message,
1115:                                        "!<?>.const_defined\\?(<?>) expected.",
1116:                                        object, constant_name)
1117:           assert_block(full_message) do
1118:             !object.const_defined?(constant_name)
1119:           end
1120:         end
1121:       end
assert_not_empty(object, message=nil) click to toggle source

Passes if object is not empty.

Example:

  assert_not_empty(" ")                      # -> pass
  assert_not_empty([nil])                    # -> pass
  assert_not_empty({1 => 2})                 # -> pass
  assert_not_empty("")                       # -> fail
  assert_not_empty([])                       # -> fail
  assert_not_empty({})                       # -> fail
      # File lib/test/unit/assertions.rb, line 1328
1328:       def assert_not_empty(object, message=nil)
1329:         _wrap_assertion do
1330:           assert_respond_to(object, :empty?,
1331:                             "The object must respond to :empty?.")
1332:           full_message = build_message(message,
1333:                                        "<?> expected to not be empty.",
1334:                                        object)
1335:           assert_block(full_message) do
1336:             not object.empty?
1337:           end
1338:         end
1339:       end
assert_not_equal(expected, actual, message="") click to toggle source

Passes if expected != actual

Example:

  assert_not_equal 'some string', 5
     # File lib/test/unit/assertions.rb, line 465
465:       def assert_not_equal(expected, actual, message="")
466:         full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
467:         assert_block(full_message) { expected != actual }
468:       end
assert_not_in_delta(expected_float, actual_float, delta=0.001, message="") click to toggle source

Passes if expected_float and actual_float are not equal within delta tolerance.

Example:

  assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00002) # -> pass
  assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00001) # -> fail
     # File lib/test/unit/assertions.rb, line 637
637:       def assert_not_in_delta(expected_float, actual_float, delta=0.001, message="")
638:         _wrap_assertion do
639:           _assert_in_delta_validate_arguments(expected_float,
640:                                               actual_float,
641:                                               delta)
642:           full_message = _assert_in_delta_message(expected_float,
643:                                                   actual_float,
644:                                                   delta,
645:                                                   message,
646:                                                   :negative_assertion => true)
647:           assert_block(full_message) do
648:             (expected_float.to_f - actual_float.to_f).abs > delta.to_f
649:           end
650:         end
651:       end
assert_not_in_epsilon(expected_float, actual_float, epsilon=0.001, message="") click to toggle source

Passes if expected_float and actual_float are not equal within epsilon relative error of expected_float.

Example:

  assert_not_in_epsilon(10000.0, 9900.0, 0.1) # -> fail
  assert_not_in_epsilon(10000.0, 9899.0, 0.1) # -> pass
     # File lib/test/unit/assertions.rb, line 765
765:       def assert_not_in_epsilon(expected_float, actual_float, epsilon=0.001,
766:                                 message="")
767:         _wrap_assertion do
768:           _assert_in_epsilon_validate_arguments(expected_float,
769:                                                 actual_float,
770:                                                 epsilon)
771:           full_message = _assert_in_epsilon_message(expected_float,
772:                                                     actual_float,
773:                                                     epsilon,
774:                                                     message,
775:                                                     :negative_assertion => true)
776:           assert_block(full_message) do
777:             normalized_expected_float = expected_float.to_f
778:             delta = normalized_expected_float * epsilon.to_f
779:             (normalized_expected_float - actual_float.to_f).abs > delta
780:           end
781:         end
782:       end
assert_not_include(collection, object, message=nil) click to toggle source

Passes if collection doesn’t include object.

Example:

  assert_not_include([1, 10], 5)            # -> pass
  assert_not_include(1..10, 20)             # -> pass
  assert_not_include([1, 10], 1)            # -> fail
  assert_not_include(1..10, 5)              # -> fail
      # File lib/test/unit/assertions.rb, line 1281
1281:       def assert_not_include(collection, object, message=nil)
1282:         _wrap_assertion do
1283:           assert_respond_to(collection, :include?,
1284:                             "The collection must respond to :include?.")
1285:           full_message = build_message(message,
1286:                                        "<?> expected to not include\n<?>.",
1287:                                        collection,
1288:                                        object)
1289:           assert_block(full_message) do
1290:             not collection.include?(object)
1291:           end
1292:         end
1293:       end
assert_not_match(regexp, string, message="") click to toggle source

Passes if regexp !~ string

Example:

  assert_not_match(/two/, 'one 2 three')   # -> pass
  assert_not_match(/three/, 'one 2 three') # -> fail
     # File lib/test/unit/assertions.rb, line 490
490:       def assert_not_match(regexp, string, message="")
491:         _wrap_assertion do
492:           assert_instance_of(Regexp, regexp,
493:                              "<REGEXP> in assert_not_match(<REGEXP>, ...) " +
494:                              "should be a Regexp.")
495:           full_message = build_message(message,
496:                                        "<?> expected to not match\n<?>.",
497:                                        regexp, string)
498:           assert_block(full_message) { regexp !~ string }
499:         end
500:       end
assert_not_nil(object, message="") click to toggle source

Passes if ! object .nil?

Example:

  assert_not_nil '1 two 3'.sub!(/two/, '2')
     # File lib/test/unit/assertions.rb, line 477
477:       def assert_not_nil(object, message="")
478:         full_message = build_message(message, "<?> expected to not be nil.", object)
479:         assert_block(full_message){!object.nil?}
480:       end
assert_not_predicate(object, predicate, message=nil) click to toggle source

Passes if object.predicate is not true.

Example:

  assert_not_predicate([1], :empty?) # -> pass
  assert_not_predicate([], :empty?)  # -> fail
      # File lib/test/unit/assertions.rb, line 1151
1151:       def assert_not_predicate(object, predicate, message=nil)
1152:         _wrap_assertion do
1153:           assert_respond_to(object, predicate, message)
1154:           actual = object.send(predicate)
1155:           full_message = build_message(message,
1156:                                        "<?>.? is false value expected but was\n" +
1157:                                        "<?>",
1158:                                        object,
1159:                                        AssertionMessage.literal(predicate),
1160:                                        actual)
1161:           assert_block(full_message) do
1162:             not actual
1163:           end
1164:         end
1165:       end
assert_not_respond_to(object, method, message="") click to toggle source

Passes if object does not .respond_to? method.

Example:

  assert_not_respond_to('bugbear', :nonexistence) # -> pass
  assert_not_respond_to('bugbear', :size)         # -> fail
     # File lib/test/unit/assertions.rb, line 319
319:       def assert_not_respond_to(object, method, message="")
320:         _wrap_assertion do
321:           full_message = build_message(message,
322:                                        "<?>.kind_of\\?(Symbol) or\n" +
323:                                        "<?>.respond_to\\?(:to_str) expected",
324:                                        method, method)
325:           assert_block(full_message) do
326:             method.kind_of?(Symbol) or method.respond_to?(:to_str)
327:           end
328:           full_message = build_message(message,
329:                                        "!<?>.respond_to\\?(?) expected\n" +
330:                                        "(Class: <?>)",
331:                                        object, method, object.class)
332:           assert_block(full_message) {!object.respond_to?(method)}
333:         end
334:       end
assert_not_same(expected, actual, message="") click to toggle source

Passes if ! actual .equal? expected

Example:

  assert_not_same Object.new, Object.new
     # File lib/test/unit/assertions.rb, line 448
448:       def assert_not_same(expected, actual, message="")
449:         full_message = build_message(message, <?>with id <?> expected to not be equal\\? to<?>with id <?>., expected, expected.__id__, actual, actual.__id__)
450:         assert_block(full_message) { !actual.equal?(expected) }
451:       end
assert_not_send(send_array, message=nil) click to toggle source

Passes if the method send doesn’t return a true value.

send_array is composed of:

  • A receiver

  • A method

  • Arguments to the method

Example:

  assert_not_send([[1, 2], :member?, 1]) # -> fail
  assert_not_send([[1, 2], :member?, 4]) # -> pass
     # File lib/test/unit/assertions.rb, line 916
916:       def assert_not_send(send_array, message=nil)
917:         _wrap_assertion do
918:           assert_instance_of(Array, send_array,
919:                              "assert_not_send requires an array " +
920:                              "of send information")
921:           assert_operator(send_array.size, :>=, 2,
922:                           "assert_not_send requires at least a receiver " +
923:                           "and a message name")
924:           format = <?> expected to respond to<?(*?)> with not a true value but was<?>.
925:           receiver, message_name, *arguments = send_array
926:           result = nil
927:           full_message =
928:             build_message(message,
929:                           format,
930:                           receiver,
931:                           AssertionMessage.literal(message_name.to_s),
932:                           arguments,
933:                           AssertionMessage.delayed_literal {result})
934:           assert_block(full_message) do
935:             result = receiver.__send__(message_name, *arguments)
936:             not result
937:           end
938:         end
939:       end
assert_nothing_raised(*args) click to toggle source

Passes if block does not raise an exception.

Example:

  assert_nothing_raised do
    [1, 2].uniq
  end
     # File lib/test/unit/assertions.rb, line 406
406:       def assert_nothing_raised(*args)
407:         _wrap_assertion do
408:           if args.last.is_a?(String)
409:             message = args.pop
410:           else
411:             message = ""
412:           end
413: 
414:           assert_exception_helper = AssertExceptionHelper.new(self, args)
415:           begin
416:             yield
417:           rescue Exception => e
418:             if ((args.empty? && !e.instance_of?(AssertionFailedError)) ||
419:                 assert_exception_helper.expected?(e))
420:               failure_message = build_message(message, "Exception raised:\n?", e)
421:               assert_block(failure_message) {false}
422:             else
423:               raise
424:             end
425:           end
426:           nil
427:         end
428:       end
assert_nothing_thrown(message="", &proc) click to toggle source

Passes if block does not throw anything.

Example:

 assert_nothing_thrown do
   [1, 2].uniq
 end
     # File lib/test/unit/assertions.rb, line 587
587:       def assert_nothing_thrown(message="", &proc)
588:         _wrap_assertion do
589:           assert(block_given?, "Should have passed a block to assert_nothing_thrown")
590:           begin
591:             proc.call
592:           rescue NameError, ArgumentError, ThreadError => error
593:             raise unless UncaughtThrow[error.class] =~ error.message
594:             tag = $1
595:             tag = tag[1..1].intern if tag[0, 1] == ":"
596:             full_message = build_message(message,
597:                                          "<?> was thrown when nothing was expected",
598:                                          tag)
599:             flunk(full_message)
600:           end
601:           assert(true, "Expected nothing to be thrown")
602:         end
603:       end
assert_operator(object1, operator, object2, message="") click to toggle source

Compares the object1 with object2 using operator.

Passes if object1.send(operator, object2) is true.

Example:

  assert_operator 5, :>=, 4
     # File lib/test/unit/assertions.rb, line 384
384:       def assert_operator(object1, operator, object2, message="")
385:         _wrap_assertion do
386:           full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
387:           assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
388:           full_message = build_message(message, <?> expected to be?<?>., object1, AssertionMessage.literal(operator), object2)
389:           assert_block(full_message) { object1.__send__(operator, object2) }
390:         end
391:       end
assert_path_exist(path, message=nil) click to toggle source

Passes if path exists.

Example:

  assert_path_exist("/tmp")          # -> pass
  assert_path_exist("/bin/sh")       # -> pass
  assert_path_exist("/nonexistent")  # -> fail
      # File lib/test/unit/assertions.rb, line 1222
1222:       def assert_path_exist(path, message=nil)
1223:         _wrap_assertion do
1224:           failure_message = build_message(message,
1225:                                           "<?> expected to exist",
1226:                                           path)
1227:           assert_block(failure_message) do
1228:             File.exist?(path)
1229:           end
1230:         end
1231:       end
assert_path_not_exist(path, message=nil) click to toggle source

Passes if path doesn’t exist.

Example:

  assert_path_not_exist("/nonexistent")  # -> pass
  assert_path_not_exist("/tmp")          # -> fail
  assert_path_not_exist("/bin/sh")       # -> fail
      # File lib/test/unit/assertions.rb, line 1240
1240:       def assert_path_not_exist(path, message=nil)
1241:         _wrap_assertion do
1242:           failure_message = build_message(message,
1243:                                           "<?> expected to not exist",
1244:                                           path)
1245:           assert_block(failure_message) do
1246:             not File.exist?(path)
1247:           end
1248:         end
1249:       end
assert_predicate(object, predicate, message=nil) click to toggle source

Passes if object.predicate is true.

Example:

  assert_predicate([], :empty?)  # -> pass
  assert_predicate([1], :empty?) # -> fail
      # File lib/test/unit/assertions.rb, line 1129
1129:       def assert_predicate(object, predicate, message=nil)
1130:         _wrap_assertion do
1131:           assert_respond_to(object, predicate, message)
1132:           actual = object.send(predicate)
1133:           full_message = build_message(message,
1134:                                        "<?>.? is true value expected but was\n" +
1135:                                        "<?>",
1136:                                        object,
1137:                                        AssertionMessage.literal(predicate),
1138:                                        actual)
1139:           assert_block(full_message) do
1140:             actual
1141:           end
1142:         end
1143:       end
assert_raise(*args, &block) click to toggle source

Passes if the block raises one of the expected exceptions. When an expected exception is an Exception object, passes if expected_exception == actual_exception.

Example:

  assert_raise(RuntimeError, LoadError) do
    raise 'Boom!!!'
  end # -> pass

  assert_raise do
    raise Exception, 'Any exception should be raised!!!'
  end # -> pass

  assert_raise(RuntimeError.new("XXX")) {raise "XXX"} # -> pass
  assert_raise(MyError.new("XXX"))      {raise "XXX"} # -> fail
  assert_raise(RuntimeError.new("ZZZ")) {raise "XXX"} # -> fail
     # File lib/test/unit/assertions.rb, line 141
141:       def assert_raise(*args, &block)
142:         assert_expected_exception = Proc.new do |*_args|
143:           message, assert_exception_helper, actual_exception = _args
144:           expected = assert_exception_helper.expected_exceptions
145:           full_message = build_message(message,
146:                                        "<?> exception expected but was\n?",
147:                                        expected, actual_exception)
148:           assert_block(full_message) do
149:             expected == [] or assert_exception_helper.expected?(actual_exception)
150:           end
151:         end
152:         _assert_raise(assert_expected_exception, *args, &block)
153:       end
assert_raise_kind_of(*args, &block) click to toggle source

Passes if the block raises one of the given exceptions or sub exceptions of the given exceptions.

Example:

  assert_raise_kind_of(SystemCallError) do
    raise Errno::EACCES
  end
     # File lib/test/unit/assertions.rb, line 173
173:       def assert_raise_kind_of(*args, &block)
174:         assert_expected_exception = Proc.new do |*_args|
175:           message, assert_exception_helper, actual_exception = _args
176:           expected = assert_exception_helper.expected_exceptions
177:           full_message = build_message(message,
178:                                        "<?> family exception expected " +
179:                                        "but was\n?",
180:                                        expected, actual_exception)
181:           assert_block(full_message) do
182:             assert_exception_helper.expected?(actual_exception, :kind_of?)
183:           end
184:         end
185:         _assert_raise(assert_expected_exception, *args, &block)
186:       end
assert_raise_message(expected, message=nil) click to toggle source

Passes if an exception is raised in block and its message is expected.

Example:

  assert_raise_message("exception") {raise "exception"}  # -> pass
  assert_raise_message(/exc/i) {raise "exception"}       # -> pass
  assert_raise_message("exception") {raise "EXCEPTION"}  # -> fail
  assert_raise_message("exception") {}                   # -> fail
      # File lib/test/unit/assertions.rb, line 1057
1057:       def assert_raise_message(expected, message=nil)
1058:         _wrap_assertion do
1059:           full_message = build_message(message,
1060:                                        "<?> exception message expected " +
1061:                                        "but none was thrown.",
1062:                                        expected)
1063:           exception = nil
1064:           assert_block(full_message) do
1065:             begin
1066:               yield
1067:               false
1068:             rescue Exception => exception
1069:               true
1070:             end
1071:           end
1072: 
1073:           actual = exception.message
1074:           diff = AssertionMessage.delayed_diff(expected, actual)
1075:           full_message =
1076:             build_message(message,
1077:                           "<?> exception message expected but was\n" +
1078:                           "<?>.?", expected, actual, diff)
1079:           assert_block(full_message) do
1080:             if expected.is_a?(Regexp)
1081:               expected =~ actual
1082:             else
1083:               expected == actual
1084:             end
1085:           end
1086:         end
1087:       end
assert_raises(*args, &block) click to toggle source

Alias of assert_raise.

Will be deprecated in 1.9, and removed in 2.0.

     # File lib/test/unit/assertions.rb, line 161
161:       def assert_raises(*args, &block)
162:         assert_raise(*args, &block)
163:       end
assert_respond_to(object, method, message="") click to toggle source

Passes if object .respond_to? method

Example:

  assert_respond_to 'bugbear', :slice
     # File lib/test/unit/assertions.rb, line 294
294:       def assert_respond_to(object, method, message="")
295:         _wrap_assertion do
296:           full_message = build_message(message,
297:                                        "<?>.kind_of\\?(Symbol) or\n" +
298:                                        "<?>.respond_to\\?(:to_str) expected",
299:                                        method, method)
300:           assert_block(full_message) do
301:             method.kind_of?(Symbol) or method.respond_to?(:to_str)
302:           end
303:           full_message = build_message(message,
304:                                        "<?>.respond_to\\?(?) expected\n" +
305:                                        "(Class: <?>)",
306:                                        object, method, object.class)
307:           assert_block(full_message) {object.respond_to?(method)}
308:         end
309:       end
assert_same(expected, actual, message="") click to toggle source

Passes if actual .equal? expected (i.e. they are the same instance).

Example:

  o = Object.new
  assert_same o, o
     # File lib/test/unit/assertions.rb, line 365
365:       def assert_same(expected, actual, message="")
366:         full_message = build_message(message, <?>with id <?> expected to be equal\\? to<?>with id <?>., expected, expected.__id__, actual, actual.__id__)
367:         assert_block(full_message) { actual.equal?(expected) }
368:       end
assert_send(send_array, message=nil) click to toggle source

Passes if the method send returns a true value.

send_array is composed of:

  • A receiver

  • A method

  • Arguments to the method

Example:

  assert_send([[1, 2], :member?, 1]) # -> pass
  assert_send([[1, 2], :member?, 4]) # -> fail
     # File lib/test/unit/assertions.rb, line 876
876:       def assert_send(send_array, message=nil)
877:         _wrap_assertion do
878:           assert_instance_of(Array, send_array,
879:                              "assert_send requires an array " +
880:                              "of send information")
881:           assert_operator(send_array.size, :>=, 2,
882:                           "assert_send requires at least a receiver " +
883:                           "and a message name")
884:           format = <?> expected to respond to<?(*?)> with a true value but was<?>.
885:           receiver, message_name, *arguments = send_array
886:           result = nil
887:           full_message =
888:             build_message(message,
889:                           format,
890:                           receiver,
891:                           AssertionMessage.literal(message_name.to_s),
892:                           arguments,
893:                           AssertionMessage.delayed_literal {result})
894:           assert_block(full_message) do
895:             result = receiver.__send__(message_name, *arguments)
896:             result
897:           end
898:         end
899:       end
assert_throw(expected_object, message="", &proc) click to toggle source

Passes if the block throws expected_object

Example:

  assert_throw(:done) do
    throw(:done)
  end
     # File lib/test/unit/assertions.rb, line 536
536:       def assert_throw(expected_object, message="", &proc)
537:         _wrap_assertion do
538:           begin
539:             catch([]) {}
540:           rescue TypeError
541:             assert_instance_of(Symbol, expected_object,
542:                                "assert_throws expects the symbol that should be thrown for its first argument")
543:           end
544:           assert_block("Should have passed a block to assert_throw.") do
545:             block_given?
546:           end
547:           caught = true
548:           begin
549:             catch(expected_object) do
550:               proc.call
551:               caught = false
552:             end
553:             full_message = build_message(message,
554:                                          "<?> should have been thrown.",
555:                                          expected_object)
556:             assert_block(full_message) {caught}
557:           rescue NameError, ArgumentError, ThreadError => error
558:             raise unless UncaughtThrow[error.class] =~ error.message
559:             tag = $1
560:             tag = tag[1..1].intern if tag[0, 1] == ":"
561:             full_message = build_message(message,
562:                                          "<?> expected to be thrown but\n" +
563:                                          "<?> was thrown.",
564:                                          expected_object, tag)
565:             flunk(full_message)
566:           end
567:         end
568:       end
assert_throws(*args, &block) click to toggle source

Alias of assert_throw.

Will be deprecated in 1.9, and removed in 2.0.

     # File lib/test/unit/assertions.rb, line 574
574:       def assert_throws(*args, &block)
575:         assert_throw(*args, &block)
576:       end
assert_true(actual, message=nil) click to toggle source

Passes if actual is true.

Example:

  assert_true(true)  # -> pass
  assert_true(:true) # -> fail
     # File lib/test/unit/assertions.rb, line 967
967:       def assert_true(actual, message=nil)
968:         _wrap_assertion do
969:           assert_block(build_message(message,
970:                                      "<true> expected but was\n<?>",
971:                                      actual)) do
972:             actual == true
973:           end
974:         end
975:       end
build_message(head, template=nil, *arguments) click to toggle source

Builds a failure message. head is added before the template and arguments replaces the ’?’s positionally in the template.

      # File lib/test/unit/assertions.rb, line 1346
1346:       def build_message(head, template=nil, *arguments)
1347:         template &&= template.chomp
1348:         return AssertionMessage.new(head, template, arguments)
1349:       end
flunk(message="Flunked") click to toggle source

Flunk always fails.

Example:

  flunk 'Not done testing yet.'
     # File lib/test/unit/assertions.rb, line 437
437:       def flunk(message="Flunked")
438:         assert_block(build_message(message)){false}
439:       end

Private Instance Methods

_wrap_assertion(&block) click to toggle source
      # File lib/test/unit/assertions.rb, line 1352
1352:       def _wrap_assertion(&block)
1353:         @_assertion_wrapped ||= false
1354:         if @_assertion_wrapped
1355:           block.call
1356:         else
1357:           @_assertion_wrapped = true
1358:           begin
1359:             add_assertion
1360:             block.call
1361:           ensure
1362:             @_assertion_wrapped = false
1363:           end
1364:         end
1365:       end
add_assertion() click to toggle source

Called whenever an assertion is made. Define this in classes that include Test::Unit::Assertions to record assertion counts.

      # File lib/test/unit/assertions.rb, line 1372
1372:       def add_assertion
1373:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.