Module | ActionController::Assertions::ResponseAssertions |
In: |
vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb
|
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.
# File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 30 30: def assert_redirected_to(options = {}, message=nil) 31: clean_backtrace do 32: assert_response(:redirect, message) 33: return true if options == @response.redirected_to 34: ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? 35: 36: begin 37: url = {} 38: original = { :expected => options, :actual => @response.redirected_to.is_a?(Symbol) ? @response.redirected_to : @response.redirected_to.dup } 39: original.each do |key, value| 40: if value.is_a?(Symbol) 41: value = @controller.respond_to?(value, true) ? @controller.send(value) : @controller.send("hash_for_#{value}_url") 42: end 43: 44: unless value.is_a?(Hash) 45: request = case value 46: when NilClass then nil 47: when /^\w+:\/\// then recognized_request_for(%r{^(\w+://.*?(/|$|\?))(.*)$} =~ value ? $3 : nil) 48: else recognized_request_for(value) 49: end 50: value = request.path_parameters if request 51: end 52: 53: if value.is_a?(Hash) # stringify 2 levels of hash keys 54: if name = value.delete(:use_route) 55: route = ActionController::Routing::Routes.named_routes[name] 56: value.update(route.parameter_shell) 57: end 58: 59: value.stringify_keys! 60: value.values.select { |v| v.is_a?(Hash) }.collect { |v| v.stringify_keys! } 61: if key == :expected && value['controller'] == @controller.controller_name && original[:actual].is_a?(Hash) 62: original[:actual].stringify_keys! 63: value.delete('controller') if original[:actual]['controller'].nil? || original[:actual]['controller'] == value['controller'] 64: end 65: end 66: 67: if value.respond_to?(:[]) && value['controller'] 68: if key == :actual && value['controller'].first != '/' && !value['controller'].include?('/') 69: value['controller'] = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path) 70: end 71: value['controller'] = value['controller'][1..-1] if value['controller'].first == '/' # strip leading hash 72: end 73: url[key] = value 74: end 75: 76: 77: @response_diff = url[:expected].diff(url[:actual]) if url[:actual] 78: msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>), difference: <?>", 79: url[:actual], @response_diff) 80: 81: assert_block(msg) do 82: url[:expected].keys.all? do |k| 83: if k == :controller then url[:expected][k] == ActionController::Routing.controller_relative_to(url[:actual][k], @controller.class.controller_path) 84: else parameterize(url[:expected][k]) == parameterize(url[:actual][k]) 85: end 86: end 87: end 88: rescue ActionController::RoutingError # routing failed us, so match the strings only. 89: msg = build_message(message, "expected a redirect to <?>, found one to <?>", options, @response.redirect_url) 90: url_regexp = %r{^(\w+://.*?(/|$|\?))(.*)$} 91: eurl, epath, url, path = [options, @response.redirect_url].collect do |url| 92: u, p = (url_regexp =~ url) ? [$1, $3] : [nil, url] 93: [u, (p.first == '/') ? p : '/' + p] 94: end.flatten 95: 96: assert_equal(eurl, url, msg) if eurl && url 97: assert_equal(epath, path, msg) if epath && path 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/response_assertions.rb, line 15 15: def assert_response(type, message = nil) 16: clean_backtrace do 17: if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?") 18: assert_block("") { true } # to count the assertion 19: elsif type.is_a?(Fixnum) && @response.response_code == type 20: assert_block("") { true } # to count the assertion 21: else 22: assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false } 23: end 24: end 25: end
Asserts that the request was rendered with the appropriate template file.
# File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 103 103: def assert_template(expected = nil, message=nil) 104: clean_backtrace do 105: rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file 106: msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered) 107: assert_block(msg) do 108: if expected.nil? 109: !@response.rendered_with_file? 110: else 111: expected == rendered 112: end 113: end 114: end 115: end