DEV Community

Author
Author

Posted on • Edited on

Minitest Assertions

Ruby on Rails 7 ships with Minitest. Here's a definitive guide to what assertions are available in Minitest 5.1.

Assertion Asserts
assert( test, [msg] ) Asserts that test is true.
assert_not( test, [msg] ) Asserts that test is false.
assert_equal( expected, actual, [msg] ) Asserts that expected == actual is true.
assert_not_equal( expected, actual, [msg] ) Asserts that expected != actual is true.
assert_same( expected, actual, [msg] ) Asserts that expected.equal?(actual) is true.
assert_not_same( expected, actual, [msg] ) Asserts that expected.equal?(actual) is false.
assert_nil( obj, [msg] ) Asserts that obj.nil? is true.
assert_not_nil( obj, [msg] ) Asserts that obj.nil? is false.
assert_empty( obj, [msg] ) Asserts that obj is empty?.
assert_not_empty( obj, [msg] ) Asserts that obj is not empty?.
assert_match( regexp, string, [msg] ) Asserts that a string matches the regular expression.
assert_no_match( regexp, string, [msg] ) Asserts that a string doesn't match the regular expression.
assert_includes( collection, obj, [msg] ) Asserts that obj is in collection.
assert_not_includes( collection, obj, [msg] ) Asserts that obj is not in collection.
assert_in_delta( expected, actual, [delta], [msg] ) Asserts that the numbers expected and actual are within delta of each other.
assert_not_in_delta( expected, actual, [delta], [msg] ) Asserts that the numbers expected and actual are not within delta of each other.
assert_in_epsilon ( expected, actual, [epsilon], [msg] ) Asserts that the numbers expected and actual have a relative error less than epsilon.
assert_not_in_epsilon ( expected, actual, [epsilon], [msg] ) Asserts that the numbers expected and actual have a relative error not less than epsilon.
assert_throws( symbol, [msg] ) { block } Asserts that the given block throws the symbol.
assert_raises( exception1, exception2, ... ) { block } Asserts that the given block raises one of the given exceptions.
assert_instance_of( class, obj, [msg] ) Asserts that obj is an instance of class.
assert_not_instance_of( class, obj, [msg] ) Asserts that obj is not an instance of class.
assert_kind_of( class, obj, [msg] ) Asserts that obj is an instance of class or is descending from it.
assert_not_kind_of( class, obj, [msg] ) Asserts that obj is not an instance of class and is not descending from it.
assert_respond_to( obj, symbol, [msg] ) Asserts that obj responds to symbol.
assert_not_respond_to( obj, symbol, [msg] ) Asserts that obj does not respond to symbol.
assert_operator( obj1, operator, [obj2], [msg] ) Asserts that obj1.operator(obj2) is true.
assert_not_operator( obj1, operator, [obj2], [msg] ) Asserts that obj1.operator(obj2) is false.
assert_predicate ( obj, predicate, [msg] ) Asserts that obj.predicate is true, e.g. assert_predicate str, :empty?
assert_not_predicate ( obj, predicate, [msg] ) Asserts that obj.predicate is false, e.g. assert_not_predicate str, :empty?
assert_error_reported(class) { block } Asserts that the error class has been reported, e.g. assert_error_reported IOError { Rails.error.report(IOError.new("Oops")) }
assert_no_error_reported { block } Asserts that no errors have been reported, e.g. assert_no_error_reported { perform_service }
flunk( [msg] ) Asserts failure. This is useful to explicitly mark a test that isn't finished yet.

We're writing Ruby on Rails at Salvation Co. Technologists keen on Saving The World are invited to join the innovations. Salvation Company

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)