Module ActionController::Assertions::ResponseAssertions
In: vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb

A small suite of assertions that test responses from Rails applications.

Methods

Public Instance methods

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.

Examples

  # assert that the redirection was to the "index" action on the WeblogController
  assert_redirected_to :controller => "weblog", :action => "index"

  # assert that the redirection was to the named route login_url
  assert_redirected_to login_url

[Source]

     # File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 53
 53:       def assert_redirected_to(options = {}, message=nil)
 54:         clean_backtrace do
 55:           assert_response(:redirect, message)
 56:           return true if options == @response.redirected_to
 57:           ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
 58: 
 59:           begin
 60:             url  = {}
 61:             original = { :expected => options, :actual => @response.redirected_to.is_a?(Symbol) ? @response.redirected_to : @response.redirected_to.dup }
 62:             original.each do |key, value|
 63:               if value.is_a?(Symbol)
 64:                 value = @controller.respond_to?(value, true) ? @controller.send(value) : @controller.send("hash_for_#{value}_url")
 65:               end
 66: 
 67:               unless value.is_a?(Hash)
 68:                 request = case value
 69:                   when NilClass    then nil
 70:                   when /^\w+:\/\// then recognized_request_for(%r{^(\w+://.*?(/|$|\?))(.*)$} =~ value ? $3 : nil)
 71:                   else                  recognized_request_for(value)
 72:                 end
 73:                 value = request.path_parameters if request
 74:               end
 75: 
 76:               if value.is_a?(Hash) # stringify 2 levels of hash keys
 77:                 if name = value.delete(:use_route)
 78:                   route = ActionController::Routing::Routes.named_routes[name]
 79:                   value.update(route.parameter_shell)
 80:                 end
 81: 
 82:                 value.stringify_keys!
 83:                 value.values.select { |v| v.is_a?(Hash) }.collect { |v| v.stringify_keys! }
 84:                 if key == :expected && value['controller'] == @controller.controller_name && original[:actual].is_a?(Hash)
 85:                   original[:actual].stringify_keys!
 86:                   value.delete('controller') if original[:actual]['controller'].nil? || original[:actual]['controller'] == value['controller']
 87:                 end
 88:               end
 89: 
 90:               if value.respond_to?(:[]) && value['controller']
 91:                 value['controller'] = value['controller'].to_s
 92:                 if key == :actual && value['controller'].first != '/' && !value['controller'].include?('/')
 93:                   new_controller_path = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path)
 94:                   value['controller'] = new_controller_path if value['controller'] != new_controller_path && ActionController::Routing.possible_controllers.include?(new_controller_path)
 95:                 end
 96:                 value['controller'] = value['controller'][1..-1] if value['controller'].first == '/' # strip leading hash
 97:               end
 98:               url[key] = value
 99:             end
100: 
101:             @response_diff = url[:actual].diff(url[:expected]) if url[:actual]
102:             msg = build_message(message, "expected a redirect to <?>, found one to <?>, a difference of <?> ", url[:expected], url[:actual], @response_diff)
103: 
104:             assert_block(msg) do
105:               url[:expected].keys.all? do |k|
106:                 if k == :controller then url[:expected][k] == ActionController::Routing.controller_relative_to(url[:actual][k], @controller.class.controller_path)
107:                 else parameterize(url[:expected][k]) == parameterize(url[:actual][k])
108:                 end
109:               end
110:             end
111:           rescue ActionController::RoutingError # routing failed us, so match the strings only.
112:             msg = build_message(message, "expected a redirect to <?>, found one to <?>", options, @response.redirect_url)
113:             url_regexp = %r{^(\w+://.*?(/|$|\?))(.*)$}
114:             eurl, epath, url, path = [options, @response.redirect_url].collect do |url|
115:               u, p = (url_regexp =~ url) ? [$1, $3] : [nil, url]
116:               [u, (p.first == '/') ? p : '/' + p]
117:             end.flatten
118: 
119:             assert_equal(eurl, url, msg) if eurl && url
120:             assert_equal(epath, path, msg) if epath && path
121:           end
122:         end
123:       end

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 number like assert_response(501) or its symbolic equivalent assert_response(:not_implemented). See ActionController::StatusCodes for a full list.

Examples

  # assert that the response was a redirection
  assert_response :redirect

  # assert that the response code was status code 401 (unauthorized)
  assert_response 401

[Source]

    # File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 27
27:       def assert_response(type, message = nil)
28:         clean_backtrace do
29:           if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?")
30:             assert_block("") { true } # to count the assertion
31:           elsif type.is_a?(Fixnum) && @response.response_code == type
32:             assert_block("") { true } # to count the assertion
33:           elsif type.is_a?(Symbol) && @response.response_code == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type]
34:             assert_block("") { true } # to count the assertion
35:           else
36:             assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
37:           end
38:         end
39:       end

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

Examples

  # assert that the "new" view template was rendered
  assert_template "new"

[Source]

     # File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 132
132:       def assert_template(expected = nil, message=nil)
133:         clean_backtrace do
134:           rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file
135:           msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered)
136:           assert_block(msg) do
137:             if expected.nil?
138:               !@response.rendered_with_file?
139:             else
140:               expected == rendered
141:             end
142:           end
143:         end
144:       end

[Validate]