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:

  • assigns: Instance variables assigned in the action that’s available for the view.
  • session: Objects being saved in the session.
  • flash: The flash objects being currently in the session.
  • cookies: Cookies being sent to the user on this request.

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 follow triggering another action call which can then be asserted against.

Methods
Public Instance methods
assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)

Asserts that the provided options can be used to generate the provided path.

     # File vendor/rails/actionpack/lib/action_controller/assertions.rb, line 98
 98:       def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
 99:         # Load routes.rb if it hasn't been loaded.
100:         ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? 
101:       
102:         # Assume given controller
103:         request = ActionController::TestRequest.new({}, {}, nil)
104:         request.path_parameters = (defaults or {}).clone
105:         request.path_parameters[:controller] ||= options[:controller]
106:       
107:         generated_path, found_extras = ActionController::Routing::Routes.generate(options, request)
108:         generated_path = generated_path.join('/')
109:         msg = build_message(message, "found extras <?>, not <?>", found_extras, extras)
110:         assert_block(msg) { found_extras == extras }
111:       
112:         msg = build_message(message, "The generated path <?> did not match <?>", generated_path, 
113:             expected_path)
114:         assert_block(msg) { expected_path == generated_path }
115:       end
assert_no_tag(opts)

Identical to assert_tag, but asserts that a matching tag does not exist. (See assert_tag for a full discussion of the syntax.)

     # File vendor/rails/actionpack/lib/action_controller/assertions.rb, line 200
200:       def assert_no_tag(opts)
201:         tag = find_tag(opts)
202:         assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}"
203:       end
assert_recognizes(expected_options, path, extras={}, message=nil)

Asserts that the routing of the given path is handled correctly and that the parsed options match.

    # File vendor/rails/actionpack/lib/action_controller/assertions.rb, line 80
80:       def assert_recognizes(expected_options, path, extras={}, message=nil)
81:         # Load routes.rb if it hasn't been loaded.
82:         ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? 
83:       
84:         # Assume given controller
85:         request = ActionController::TestRequest.new({}, {}, nil)
86:         request.path = path
87:         ActionController::Routing::Routes.recognize!(request)
88:       
89:         expected_options = expected_options.clone
90:         extras.each_key { |key| expected_options.delete key } unless extras.nil?
91:       
92:         msg = build_message(message, "The recognized options <?> did not match <?>", 
93:             request.path_parameters, expected_options)
94:         assert_block(msg) { request.path_parameters == expected_options }
95:       end
assert_redirected_to(options = {}, 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 at assert_redirected_to(:controller => "weblog") will also match the redirection of redirect_to(:controller => "weblog", :action => "show") and so on.

    # File vendor/rails/actionpack/lib/action_controller/assertions.rb, line 51
51:       def assert_redirected_to(options = {}, message=nil)
52:         assert_redirect(message)
53: 
54:         msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>)", @response.redirected_to)
55:         assert_block(msg) do
56:           if options.is_a?(Symbol)
57:             @response.redirected_to == options
58:           else
59:             options.keys.all? do |k| 
60:               options[k] == (@response.redirected_to[k].respond_to?(:to_param) ? @response.redirected_to[k].to_param : @response.redirected_to[k] if @response.redirected_to[k])
61:             end
62:           end
63:         end
64:       end
assert_response(type, message = nil)

Asserts that the response is one of the following types:

  • :success: Status code was 200
  • :redirect: Status code was in the 300-399 range
  • :missing: Status code was 404
  • :error: Status code was in the 500-599 range

You can also pass an explicit status code number as the type, like assert_response(501)

    # File vendor/rails/actionpack/lib/action_controller/assertions.rb, line 38
38:       def assert_response(type, message = nil)
39:         if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?")
40:           assert_block("") { true } # to count the assertion
41:         elsif type.is_a?(Fixnum) && @response.response_code == type
42:           assert_block("") { true } # to count the assertion
43:         else
44:           assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
45:         end
46:       end
assert_routing(path, options, defaults={}, extras={}, message=nil)

asserts that path and options match both ways, in other words, the URL generated from options is same as path, and also that the options recognized from path are same as options

     # File vendor/rails/actionpack/lib/action_controller/assertions.rb, line 119
119:       def assert_routing(path, options, defaults={}, extras={}, message=nil)
120:         assert_recognizes(options, path, extras, message)
121:         assert_generates(path, options, defaults, extras, message)
122:       end
assert_tag(opts)

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):

  • :tag: the node type must match the corresponding value
  • :attributes: a hash. The node’s attributes must match the corresponding values in the hash.
  • :parent: a hash. The node’s parent must match the corresponding hash.
  • :child: a hash. At least one of the node’s immediate children must meet the criteria described by the hash.
  • :ancestor: a hash. At least one of the node’s ancestors must meet the criteria described by the hash.
  • :descendant: a hash. At least one of the node’s descendants must meet the criteria described by the hash.
  • :children: a hash, for counting children of a node. Accepts the keys:
    • :count: either a number or a range which must equal (or include) the number of children that match.
    • :less_than: the number of matching children must be less than this number.
    • :greater_than: the number of matching children must be greater than this number.
    • :only: another hash consisting of the keys to use to match on the children, and only matching children will be counted.
  • :content: (text nodes only). The content of the node must match the given value.

Conditions are matched using the following algorithm:

  • if the condition is a string, it must be a substring of the value.
  • if the condition is a regexp, it must match the value.
  • if the condition is a number, the value must match number.to_s.
  • if the condition is true, the value must not be nil.
  • if the condition is false or nil, the value must be nil.

Usage:

  # assert that there is a "span" tag
  assert_tag :tag => "span"

  # 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/ }
     # File vendor/rails/actionpack/lib/action_controller/assertions.rb, line 193
193:       def assert_tag(opts)
194:         tag = find_tag(opts)
195:         assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}"
196:       end
assert_template(expected = nil, message=nil)

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

    # File vendor/rails/actionpack/lib/action_controller/assertions.rb, line 67
67:       def assert_template(expected = nil, message=nil)
68:         rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file
69:         msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered)
70:         assert_block(msg) do
71:           if expected.nil?
72:             @response.rendered_with_file?
73:           else
74:             expected == rendered
75:           end
76:         end
77:       end