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

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.

[Source]

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

[Source]

    # File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 17
17:       def assert_response(type, message = nil)
18:         clean_backtrace do
19:           if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?")
20:             assert_block("") { true } # to count the assertion
21:           elsif type.is_a?(Fixnum) && @response.response_code == type
22:             assert_block("") { true } # to count the assertion
23:           elsif type.is_a?(Symbol) && @response.response_code == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type]
24:             assert_block("") { true } # to count the assertion
25:           else
26:             assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
27:           end
28:         end
29:       end

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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 108
108:       def assert_template(expected = nil, message=nil)
109:         clean_backtrace do
110:           rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file
111:           msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered)
112:           assert_block(msg) do
113:             if expected.nil?
114:               !@response.rendered_with_file?
115:             else
116:               expected == rendered
117:             end
118:           end
119:         end
120:       end

[Validate]