Module | Test::Unit::Assertions |
In: |
vendor/rails/actionpack/lib/action_controller/assertions.rb
vendor/rails/actionpack/lib/action_controller/deprecated_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 follow 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 happen. But some times you also want to manipulate these collections in the request coming in. 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 |
Asserts that the provided options can be used to generate the provided path.
# File vendor/rails/actionpack/lib/action_controller/assertions.rb, line 136 136: def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil) 137: expected_path = "/#{expected_path}" unless expected_path[0] == ?/ 138: # Load routes.rb if it hasn't been loaded. 139: ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? 140: 141: # Assume given controller 142: request = ActionController::TestRequest.new({}, {}, nil) 143: request.path_parameters = (defaults or {}).clone 144: request.path_parameters[:controller] ||= options[:controller] 145: 146: generated_path, found_extras = ActionController::Routing::Routes.generate(options, request) 147: msg = build_message(message, "found extras <?>, not <?>", found_extras, extras) 148: assert_block(msg) { found_extras == extras } 149: 150: msg = build_message(message, "The generated path <?> did not match <?>", generated_path, 151: expected_path) 152: assert_block(msg) { expected_path == generated_path } 153: end
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 245 245: def assert_no_tag(opts) 246: tag = find_tag(opts) 247: assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}" 248: end
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 116 116: def assert_recognizes(expected_options, path, extras={}, message=nil) 117: path = "/#{path}" unless path[0..0] == '/' 118: # Load routes.rb if it hasn't been loaded. 119: ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? 120: 121: # Assume given controller 122: request = ActionController::TestRequest.new({}, {}, nil) 123: request.path = path 124: ActionController::Routing::Routes.recognize!(request) 125: 126: expected_options = expected_options.clone 127: extras.each_key { |key| expected_options.delete key } unless extras.nil? 128: 129: expected_options.stringify_keys! 130: msg = build_message(message, "The recognized options <?> did not match <?>", 131: request.path_parameters, expected_options) 132: assert_block(msg) { request.path_parameters == expected_options } 133: end
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 70 70: def assert_redirected_to(options = {}, message=nil) 71: assert_response(:redirect, message) 72: 73: if options.is_a?(String) 74: msg = build_message(message, "expected a redirect to <?>, found one to <?>", options, @response.redirect_url) 75: url_regexp = %r{^(\w+://.*?(/|$|\?))(.*)$} 76: eurl, epath, url, path = [options, @response.redirect_url].collect do |url| 77: u, p = (url_regexp =~ url) ? [$1, $3] : [nil, url] 78: [u, (p[0..0] == '/') ? p : '/' + p] 79: end.flatten 80: 81: if eurl && url then assert_equal(eurl, url, msg) 82: else assert_equal(epath, path, msg) 83: end 84: else 85: msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>)", 86: @response.redirected_to || @response.redirect_url) 87: 88: assert_block(msg) do 89: if options.is_a?(Symbol) 90: @response.redirected_to == options 91: else 92: options.keys.all? do |k| 93: if k == :controller then options[k] == ActionController::Routing.controller_relative_to(@response.redirected_to[k], @controller.class.controller_path) 94: else options[k] == (@response.redirected_to[k].respond_to?(:to_param) ? @response.redirected_to[k].to_param : @response.redirected_to[k] unless @response.redirected_to[k].nil?) 95: end 96: end 97: end 98: end 99: end 100: end
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)
# File vendor/rails/actionpack/lib/action_controller/assertions.rb, line 57 57: def assert_response(type, message = nil) 58: if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?") 59: assert_block("") { true } # to count the assertion 60: elsif type.is_a?(Fixnum) && @response.response_code == type 61: assert_block("") { true } # to count the assertion 62: else 63: assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false } 64: end 65: end
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 157 157: def assert_routing(path, options, defaults={}, extras={}, message=nil) 158: assert_recognizes(options, path, extras, message) 159: assert_generates(path, options, defaults, extras, message) 160: end
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" 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 238 238: def assert_tag(opts) 239: tag = find_tag(opts) 240: assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}" 241: end
Asserts that the request was rendered with the appropriate template file.
# File vendor/rails/actionpack/lib/action_controller/assertions.rb, line 103 103: def assert_template(expected = nil, message=nil) 104: rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file 105: msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered) 106: assert_block(msg) do 107: if expected.nil? 108: !@response.rendered_with_file? 109: else 110: expected == rendered 111: end 112: end 113: end