| 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.
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.
# 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
# File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 59
59: def assert_redirected_to(options = {}, message=nil)
60: clean_backtrace do
61: assert_response(:redirect, message)
62: return true if options == @response.redirected_to
63: ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
64:
65: begin
66: url = {}
67: original = { :expected => options, :actual => @response.redirected_to.is_a?(Symbol) ? @response.redirected_to : @response.redirected_to.dup }
68: original.each do |key, value|
69: if value.is_a?(Symbol)
70: value = @controller.respond_to?(value, true) ? @controller.send(value) : @controller.send("hash_for_#{value}_url")
71: end
72:
73: unless value.is_a?(Hash)
74: request = case value
75: when NilClass then nil
76: when /^\w+:\/\// then recognized_request_for(%r{^(\w+://.*?(/|$|\?))(.*)$} =~ value ? $3 : nil)
77: else recognized_request_for(value)
78: end
79: value = request.path_parameters if request
80: end
81:
82: if value.is_a?(Hash) # stringify 2 levels of hash keys
83: if name = value.delete(:use_route)
84: route = ActionController::Routing::Routes.named_routes[name]
85: value.update(route.parameter_shell)
86: end
87:
88: value.stringify_keys!
89: value.values.select { |v| v.is_a?(Hash) }.collect { |v| v.stringify_keys! }
90: if key == :expected && value['controller'] == @controller.controller_name && original[:actual].is_a?(Hash)
91: original[:actual].stringify_keys!
92: value.delete('controller') if original[:actual]['controller'].nil? || original[:actual]['controller'] == value['controller']
93: end
94: end
95:
96: if value.respond_to?(:[]) && value['controller']
97: value['controller'] = value['controller'].to_s
98: if key == :actual && value['controller'].first != '/' && !value['controller'].include?('/')
99: new_controller_path = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path)
100: value['controller'] = new_controller_path if value['controller'] != new_controller_path && ActionController::Routing.possible_controllers.include?(new_controller_path)
101: end
102: value['controller'] = value['controller'][1..-1] if value['controller'].first == '/' # strip leading hash
103: end
104: url[key] = value
105: end
106:
107: @response_diff = url[:actual].diff(url[:expected]) if url[:actual]
108: msg = build_message(message, "expected a redirect to <?>, found one to <?>, a difference of <?> ", url[:expected], url[:actual], @response_diff)
109:
110: assert_block(msg) do
111: url[:expected].keys.all? do |k|
112: if k == :controller then url[:expected][k] == ActionController::Routing.controller_relative_to(url[:actual][k], @controller.class.controller_path)
113: else parameterize(url[:expected][k]) == parameterize(url[:actual][k])
114: end
115: end
116: end
117: rescue ActionController::RoutingError # routing failed us, so match the strings only.
118: msg = build_message(message, "expected a redirect to <?>, found one to <?>", options, @response.redirect_url)
119: url_regexp = %r{^(\w+://.*?(/|$|\?))(.*)$}
120: eurl, epath, url, path = [options, @response.redirect_url].collect do |url|
121: u, p = (url_regexp =~ url) ? [$1, $3] : [nil, url]
122: [u, (p.first == '/') ? p : '/' + p]
123: end.flatten
124:
125: assert_equal(eurl, url, msg) if eurl && url
126: assert_equal(epath, path, msg) if epath && path
127: end
128: end
129: end
Asserts that the response is one of the following types:
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.
# assert that the response was a redirection assert_response :redirect # assert that the response code was status code 401 (unauthorized) assert_response 401
# 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: if @response.error?
37: exception = @response.template.instance_variable_get(:@exception)
38: exception_message = exception && exception.message
39: assert_block(build_message(message, "Expected response to be a <?>, but was <?>\n<?>", type, @response.response_code, exception_message.to_s)) { false }
40: else
41: assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
42: end
43: end
44: end
45: end
Asserts that the request was rendered with the appropriate template file.
# assert that the "new" view template was rendered assert_template "new"
# File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 138
138: def assert_template(expected = nil, message=nil)
139: clean_backtrace do
140: rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file
141: msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered)
142: assert_block(msg) do
143: if expected.nil?
144: !@response.rendered_with_file?
145: else
146: expected == rendered
147: end
148: end
149: end
150: end