DEV Community

Innovation Leadership
Innovation Leadership

Posted on

Minitest Rails Assertions (table)

Minitest is the default testing library used by Ruby on Rails. Here's a synopsis of the available api.

Available Assertions

From Minitest:

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

From Rails:

Assertion Purpose
assert_difference(expressions, difference = 1, message = nil) {...} Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
assert_no_difference(expressions, message = nil, &block) Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
assert_changes(expressions, message = nil, from:, to:, &block) Test that the result of evaluating an expression is changed after invoking the passed in block.
assert_no_changes(expressions, message = nil, &block) Test the result of evaluating an expression is not changed after invoking the passed in block.
assert_nothing_raised { block } Ensures that the given block doesn't raise any exceptions.
assert_recognizes(expected_options, path, extras={}, message=nil) Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.
assert_generates(expected_path, options, defaults={}, extras = {}, message=nil) Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.
assert_response(type, message = nil) Asserts that the response comes with a specific status code. You can specify :success to indicate 200-299, :redirect to indicate 300-399, :missing to indicate 404, or :error to match the 500-599 range. You can also pass an explicit status number or its symbolic equivalent. For more information, see full list of status codes and how their mapping works.
assert_redirected_to(options = {}, message=nil) Asserts that the response is a redirect to a URL matching the given options. You can also pass named routes such as assert_redirected_to root_path and Active Record objects such as assert_redirected_to @article.
assert_queries_count(count = nil, include_schema: false, &block) Asserts that &block generates an int number of SQL queries.
assert_no_queries(include_schema: false, &block) Asserts that &block generates no SQL queries.
assert_queries_match(pattern, count: nil, include_schema: false, &block) Asserts that &block generates SQL queries that match the pattern.
assert_no_queries_match(pattern, &block) Asserts that &block generates no SQL queries that match the pattern.

Here's that thing you need. I got it from guides.rubyonrails.org/testing.html.

Top comments (0)