From 57313c54bfc3a47f32507cb466df91b5a9688db7 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 7 Nov 2006 21:45:10 +0000 Subject: assert_response supports symbolic status codes. Closes #6569. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5466 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../assertions/response_assertions.rb | 26 +++++++++++++--------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/assertions/response_assertions.rb b/actionpack/lib/action_controller/assertions/response_assertions.rb index a3834e3a3e..c0b78552f2 100644 --- a/actionpack/lib/action_controller/assertions/response_assertions.rb +++ b/actionpack/lib/action_controller/assertions/response_assertions.rb @@ -5,27 +5,31 @@ module ActionController module Assertions module ResponseAssertions # 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 code number as the type, like assert_response(501) + # 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. def assert_response(type, message = nil) clean_backtrace do if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?") assert_block("") { true } # to count the assertion elsif type.is_a?(Fixnum) && @response.response_code == type assert_block("") { true } # to count the assertion + elsif type.is_a?(Symbol) && @response.response_code == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type] + assert_block("") { true } # to count the assertion else assert_block(build_message(message, "Expected response to be a , but was ", type, @response.response_code)) { false } - end + end end end # 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 + # such that assert_redirected_to(:controller => "weblog") will also match the redirection of # redirect_to(:controller => "weblog", :action => "show") and so on. def assert_redirected_to(options = {}, message=nil) clean_backtrace do @@ -66,18 +70,18 @@ module ActionController if value.respond_to?(:[]) && value['controller'] if key == :actual && value['controller'].first != '/' && !value['controller'].include?('/') - value['controller'] = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path) + value['controller'] = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path) end value['controller'] = value['controller'][1..-1] if value['controller'].first == '/' # strip leading hash end url[key] = value end - + @response_diff = url[:expected].diff(url[:actual]) if url[:actual] - msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is ), difference: ", + msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is ), difference: ", url[:actual], @response_diff) - + assert_block(msg) do url[:expected].keys.all? do |k| if k == :controller then url[:expected][k] == ActionController::Routing.controller_relative_to(url[:actual][k], @controller.class.controller_path) @@ -94,7 +98,7 @@ module ActionController end.flatten assert_equal(eurl, url, msg) if eurl && url - assert_equal(epath, path, msg) if epath && path + assert_equal(epath, path, msg) if epath && path end end end @@ -110,7 +114,7 @@ module ActionController else expected == rendered end - end + end end end @@ -132,4 +136,4 @@ module ActionController end end end -end \ No newline at end of file +end -- cgit v1.2.3