Module | Test::Unit::Assertions |
In: |
lib/action_controller/deprecated_assertions.rb
lib/action_controller/assertions.rb |
In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions can be used against. These collections are:
These collections can be used just like any other hash:
assert_not_nil assigns(:person) # makes sure that a @person instance variable was set assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave" assert flash.empty? # makes sure that there's nothing in the flash
For historic reasons, the assigns hash uses string-based keys. So assigns[:person] won‘t work, but assigns["person"] will. To appease our yearning for symbols, though, an alternative accessor has been deviced using a method call instead of index referencing. So assigns(:person) will work just like assigns["person"], but again, assigns[:person] will not work.
On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url.
For redirects within the same controller, you can even call follow_redirect and the redirect will be followed, triggering another action call which can then be asserted against.
The collections described above link to the response, so you can test if what the actions were expected to do happened. But sometimes you also want to manipulate these collections in the incoming request. This is really only relevant for sessions and cookies, though. For sessions, you just do:
@request.session[:key] = "value"
For cookies, you need to manually create the cookie, like this:
@request.cookies["key"] = CGI::Cookie.new("key", "value")
If you‘re using named routes, they can be easily tested using the original named routes methods straight in the test case. Example:
assert_redirected_to page_url(:title => 'foo')
assert_template_equal | -> | assert_assigned_equal |
Identical to assert_tag, but asserts that a matching tag does not exist. (See assert_tag for a full discussion of the syntax.)
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.
Asserts that the response is one of the following types:
You can also pass an explicit status code number as the type, like assert_response(501)
Asserts that path and options match both ways; in other words, the URL generated from options is the same as path, and also that the options recognized from path are the same as options
Asserts that there is a tag/node/element in the body of the response that meets all of the given conditions. The conditions parameter must be a hash of any of the following keys (all are optional):
given value. This will not match HTML tags in the body of a tag--only text.
Conditions are matched using the following algorithm:
Usage:
# assert that there is a "span" tag assert_tag :tag => "span" # assert that there is a "span" tag with id="x" assert_tag :tag => "span", :attributes => { :id => "x" } # assert that there is a "span" tag using the short-hand assert_tag :span # assert that there is a "span" tag with id="x" using the short-hand assert_tag :span, :attributes => { :id => "x" } # assert that there is a "span" inside of a "div" assert_tag :tag => "span", :parent => { :tag => "div" } # assert that there is a "span" somewhere inside a table assert_tag :tag => "span", :ancestor => { :tag => "table" } # assert that there is a "span" with at least one "em" child assert_tag :tag => "span", :child => { :tag => "em" } # assert that there is a "span" containing a (possibly nested) # "strong" tag. assert_tag :tag => "span", :descendant => { :tag => "strong" } # assert that there is a "span" containing between 2 and 4 "em" tags # as immediate children assert_tag :tag => "span", :children => { :count => 2..4, :only => { :tag => "em" } } # get funky: assert that there is a "div", with an "ul" ancestor # and an "li" parent (with "class" = "enum"), and containing a # "span" descendant that contains text matching /hello world/ assert_tag :tag => "div", :ancestor => { :tag => "ul" }, :parent => { :tag => "li", :attributes => { :class => "enum" } }, :descendant => { :tag => "span", :child => /hello world/ }
<strong>Please note</strong: assert_tag and assert_no_tag only work with well-formed XHTML. They recognize a few tags as implicitly self-closing (like br and hr and such) but will not work correctly with tags that allow optional closing tags (p, li, td). You must explicitly close all of your tags to use these assertions.