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 # assert that the redirection was to the url for @customer assert_redirected_to @customer
# File vendor/rails/actionpack/lib/action_controller/assertions/response_assertions.rb, line 62 62: def assert_redirected_to(options = {}, message=nil) 63: clean_backtrace do 64: assert_response(:redirect, message) 65: return true if options == @response.redirected_to 66: 67: # Support partial arguments for hash redirections 68: if options.is_a?(Hash) && @response.redirected_to.is_a?(Hash) 69: return true if options.all? {|(key, value)| @response.redirected_to[key] == value} 70: end 71: 72: redirected_to_after_normalisation = normalize_argument_to_redirection(@response.redirected_to) 73: options_after_normalisation = normalize_argument_to_redirection(options) 74: 75: if redirected_to_after_normalisation != options_after_normalisation 76: flunk "Expected response to be a redirect to <#{options_after_normalisation}> but was a redirect to <#{redirected_to_after_normalisation}>" 77: end 78: end 79: 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 88 88: def assert_template(expected = nil, message=nil) 89: clean_backtrace do 90: rendered = @response.rendered_template.to_s 91: msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered) 92: assert_block(msg) do 93: if expected.nil? 94: @response.rendered_template.blank? 95: else 96: rendered.to_s.match(expected) 97: end 98: end 99: end 100: end