Skip to content

Instantly share code, notes, and snippets.

@plusor
Last active December 20, 2015 08:59
Show Gist options
  • Save plusor/6104632 to your computer and use it in GitHub Desktop.
Save plusor/6104632 to your computer and use it in GitHub Desktop.
Rails Assertions

Ensures that the object/expression is true.

  • assert( boolean, [msg] )

Ensures that obj1 == obj2 is true.

  • assert_equal( obj1, obj2, [msg] )

Ensures that obj1 == obj2 is false.

  • assert_not_equal( obj1, obj2, [msg] )

Ensures that obj1.equal?(obj2) is true.

  • assert_same( obj1, obj2, [msg] )

Ensures that obj1.equal?(obj2) is false.

  • assert_not_same( obj1, obj2, [msg] )

Ensures that obj.nil? is true.

  • assert_nil( obj, [msg] )

Ensures that obj.nil? is false.

  • assert_not_nil( obj, [msg] )

Ensures that a string matches the regular expression.

  • assert_match( regexp, string, [msg] )

Ensures that a string doesn’t match the regular expression.

  • assert_no_match( regexp, string, [msg] )

Ensures that the numbers expecting and actual are within delta of each other.

  • assert_in_delta( expecting, actual, delta, [msg] )

Ensures that the given block throws the symbol.

  • assert_throws( symbol, [msg] ) { block }

Ensures that the given block raises one of the given exceptions.

  • assert_raise( exception1, exception2, ... ) { block }

Ensures that the given block doesn’t raise one of the given exceptions.

  • assert_nothing_raised( exception1, exception2, ... ) { block }

Ensures that obj is of the class type.

  • assert_instance_of( class, obj, [msg] )

Ensures that obj is or descends from class.

  • assert_kind_of( class, obj, [msg] )

Ensures that obj has a method called symbol.

  • assert_respond_to( obj, symbol, [msg] )

Ensures that obj1.operator(obj2) is true.

  • assert_operator( obj1, operator, obj2, [msg] )

Ensures that executing the method listed in array[1] on the object in array[0] with the parameters of array[2 and up] is true. This one is weird eh?

  • assert_send( array, [msg] )

Ensures failure. This is useful to explicitly mark a test that isn’t finished yet.

  • flunk( [msg] )

Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.

  • assert_valid(record)

Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.

  • assert_difference(expressions, difference = 1, message = nil) {...}

Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.

  • assert_no_difference(expressions, message = nil, &block)

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_recognizes(expected_options, path, 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_generates(expected_path, options, defaults={}, extras = {}, message=nil)

Asserts that the response comes with a specific status code. You can specify :success to indicate 200, :redirect to indicate 300-399, :missing to indicate 404, or :error to match the 500-599 range

  • assert_response(type, message = nil)

Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that assert_redirected_to(:controller => "weblog") will also match the redirection of redirect_to(:controller => "weblog", :action => "show") and so on.

  • assert_redirected_to(options = {}, message=nil)

Asserts that the request was rendered with the appropriate template file.

  • assert_template(expected = nil, message=nil)

##Additional View-Based Assertions

Allows you to make assertions on the body of an e-mail.

  • assert_select_email

Allows you to make assertions on encoded HTML. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.

  • assert_select_encoded

Returns an array of all the elements selected by the selector. In the second variant it first matches the base element and tries to match the selector expression on any of its children. If there are no matches both variants return an empty array.

  • css_select(selector) or css_select(element, selector)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment