Changeset 1189
- Timestamp:
- 04/17/05 11:38:01 (3 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/assertions (deleted)
- trunk/actionpack/lib/action_controller/assertions.rb (copied) (copied from trunk/actionpack/lib/action_controller/assertions/action_pack_assertions.rb) (5 diffs)
- trunk/actionpack/lib/action_controller/deprecated_assertions.rb (added)
- trunk/actionpack/lib/action_controller/flash.rb (modified) (3 diffs)
- trunk/actionpack/lib/action_controller/test_process.rb (modified) (4 diffs)
- trunk/actionpack/lib/action_view/base.rb (modified) (2 diffs)
- trunk/actionpack/test/controller/action_pack_assertions_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r1184 r1189 1 1 *SVN* 2 3 * Deprecated the majority of all the testing assertions and replaced them with a much smaller core and access to all the collections the old assertions relied on. That way the regular test/unit assertions can be used against these. Added documentation about how to use it all. 2 4 3 5 * Fixed DateHelper to return values on the option tags such that they'll work properly in IE with form_remote_tag #1024 [rscottmace@gmail.com] trunk/actionpack/lib/action_controller/assertions.rb
r1065 r1189 5 5 module Test #:nodoc: 6 6 module Unit #:nodoc: 7 # Adds a wealth of assertions to do functional testing of Action Controllers. 7 # In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions 8 # can be used against. These collections are: 9 # 10 # * assigns: Instance variables assigned in the action that's available for the view. 11 # * session: Objects being saved in the session. 12 # * flash: The flash objects being currently in the session. 13 # * cookies: Cookies being sent to the user on this request. 14 # 15 # These collections can be used just like any other hash: 16 # 17 # assert_not_nil assigns[:person] # makes sure that a @person instance variable was set 18 # assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave" 19 # assert flash.empty? # makes sure that there's nothing in the flash 20 # 21 # On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url. 22 # 23 # For redirects within the same controller, you can even call follow_redirect and the redirect will be follow triggering another 24 # action call which can then be asserted against. 8 25 module Assertions 9 # -- basic assertions --------------------------------------------------- 10 11 # ensure that the web request has been serviced correctly 12 def assert_success(message=nil) 13 response = acquire_assertion_target 14 if response.success? 15 # to count the assertion 16 assert_block("") { true } 26 # Asserts that the response is one of the following types: 27 # 28 # * <tt>:success</tt>: Status code was 200 29 # * <tt>:redirect</tt>: Status code was in the 300-399 range 30 # * <tt>:missing</tt>: Status code was 404 31 # * <tt>:error</tt>: Status code was in the 500-599 range 32 # 33 # You can also pass an explicit status code number as the type, like assert_response(501) 34 def assert_response(type, message = nil) 35 if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?") 36 assert_block("") { true } # to count the assertion 37 elsif type.is_a?(Fixnum) && @response.response_code == type 38 assert_block("") { true } # to count the assertion 17 39 else 18 if response.redirect? 19 msg = build_message(message, "Response unexpectedly redirect to <?>", response.redirect_url) 20 else 21 msg = build_message(message, "unsuccessful request (response code = <?>)", 22 response.response_code) 23 end 24 assert_block(msg) { false } 40 assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false } 25 41 end 26 42 end 27 43 28 # ensure the request was rendered with the appropriate template file 29 def assert_rendered_file(expected=nil, message=nil) 30 response = acquire_assertion_target 31 rendered = expected ? response.rendered_file(!expected.include?('/')) : response.rendered_file 44 # Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, 45 # such at assert_redirected_to(:controller => "weblog") will also match the redirection of 46 # redirect_to(:controller => "weblog", :action => "show") and so on. 47 def assert_redirected_to(options = {}, message=nil) 48 assert_redirect(message) 49 50 msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>)", @response.redirected_to) 51 assert_block(msg) do 52 if options.is_a?(Symbol) 53 @response.redirected_to == options 54 else 55 options.keys.all? do |k| 56 options[k] == (@response.redirected_to[k].respond_to?(:to_param) ? @response.redirected_to[k].to_param : @response.redirected_to[k] if @response.redirected_to[k]) 57 end 58 end 59 end 60 end 61 62 # Asserts that the request was rendered with the appropriate template file 63 def assert_template(expected=nil, message=nil) 64 rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file 32 65 msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered) 33 66 assert_block(msg) do 34 67 if expected.nil? 35 response.rendered_with_file?68 @response.rendered_with_file? 36 69 else 37 70 expected == rendered … … 39 72 end 40 73 end 74 75 alias_method :assert_rendered_file, :assert_template #:nodoc: 41 76 42 # -- session assertions -------------------------------------------------43 44 # ensure that the session has an object with the specified name45 def assert_session_has(key=nil, message=nil)46 response = acquire_assertion_target47 msg = build_message(message, "<?> is not in the session <?>", key, response.session)48 assert_block(msg) { response.has_session_object?(key) }49 end50 51 # ensure that the session has no object with the specified name52 def assert_session_has_no(key=nil, message=nil)53 response = acquire_assertion_target54 msg = build_message(message, "<?> is in the session <?>", key, response.session)55 assert_block(msg) { !response.has_session_object?(key) }56 end57 58 def assert_session_equal(expected = nil, key = nil, message = nil)59 response = acquire_assertion_target60 msg = build_message(message, "<?> expected in session['?'] but was <?>", expected, key, response.session[key])61 assert_block(msg) { expected == response.session[key] }62 end63 64 # -- cookie assertions ---------------------------------------------------65 66 def assert_no_cookie(key = nil, message = nil)67 response = acquire_assertion_target68 actual = response.cookies[key]69 msg = build_message(message, "<?> not expected in cookies['?']", actual, key)70 assert_block(msg) { actual.nil? or actual.empty? }71 end72 73 def assert_cookie_equal(expected = nil, key = nil, message = nil)74 response = acquire_assertion_target75 actual = response.cookies[key]76 actual = actual.first if actual77 msg = build_message(message, "<?> expected in cookies['?'] but was <?>", expected, key, actual)78 assert_block(msg) { expected == actual }79 end80 81 # -- flash assertions ---------------------------------------------------82 83 # ensure that the flash has an object with the specified name84 def assert_flash_has(key=nil, message=nil)85 response = acquire_assertion_target86 msg = build_message(message, "<?> is not in the flash <?>", key, response.flash)87 assert_block(msg) { response.has_flash_object?(key) }88 end89 90 # ensure that the flash has no object with the specified name91 def assert_flash_has_no(key=nil, message=nil)92 response = acquire_assertion_target93 msg = build_message(message, "<?> is in the flash <?>", key, response.flash)94 assert_block(msg) { !response.has_flash_object?(key) }95 end96 97 # ensure the flash exists98 def assert_flash_exists(message=nil)99 response = acquire_assertion_target100 msg = build_message(message, "the flash does not exist <?>", response.session['flash'] )101 assert_block(msg) { response.has_flash? }102 end103 104 # ensure the flash does not exist105 def assert_flash_not_exists(message=nil)106 response = acquire_assertion_target107 msg = build_message(message, "the flash exists <?>", response.flash)108 assert_block(msg) { !response.has_flash? }109 end110 111 # ensure the flash is empty but existent112 def assert_flash_empty(message=nil)113 response = acquire_assertion_target114 msg = build_message(message, "the flash is not empty <?>", response.flash)115 assert_block(msg) { !response.has_flash_with_contents? }116 end117 118 # ensure the flash is not empty119 def assert_flash_not_empty(message=nil)120 response = acquire_assertion_target121 msg = build_message(message, "the flash is empty")122 assert_block(msg) { response.has_flash_with_contents? }123 end124 125 def assert_flash_equal(expected = nil, key = nil, message = nil)126 response = acquire_assertion_target127 msg = build_message(message, "<?> expected in flash['?'] but was <?>", expected, key, response.flash[key])128 assert_block(msg) { expected == response.flash[key] }129 end130 131 # -- redirection assertions ---------------------------------------------132 133 # ensure we have be redirected134 def assert_redirect(message=nil)135 response = acquire_assertion_target136 msg = build_message(message, "response is not a redirection (response code is <?>)", response.response_code)137 assert_block(msg) { response.redirect? }138 end139 140 def assert_redirected_to(options = {}, message=nil)141 assert_redirect(message)142 response = acquire_assertion_target143 144 msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>)", response.redirected_to)145 assert_block(msg) do146 if options.is_a?(Symbol)147 response.redirected_to == options148 else149 options.keys.all? { |k| options[k] == ( response.redirected_to[k].respond_to?(:to_param) ? response.redirected_to[k].to_param : response.redirected_to[k] if response.redirected_to[k] ) }150 end151 end152 end153 154 # ensure our redirection url is an exact match155 def assert_redirect_url(url=nil, message=nil)156 assert_redirect(message)157 response = acquire_assertion_target158 msg = build_message(message, "<?> is not the redirected location <?>", url, response.redirect_url)159 assert_block(msg) { response.redirect_url == url }160 end161 162 # ensure our redirection url matches a pattern163 def assert_redirect_url_match(pattern=nil, message=nil)164 assert_redirect(message)165 response = acquire_assertion_target166 msg = build_message(message, "<?> was not found in the location: <?>", pattern, response.redirect_url)167 assert_block(msg) { response.redirect_url_match?(pattern) }168 end169 170 77 # -- routing assertions -------------------------------------------------- 171 78 … … 174 81 # Load routes.rb if it hasn't been loaded. 175 82 ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? 176 83 177 84 # Assume given controller 178 85 request = ActionController::TestRequest.new({}, {}, nil) 179 86 request.path = path 180 87 ActionController::Routing::Routes.recognize!(request) 181 88 182 89 expected_options = expected_options.clone 183 90 extras.each_key { |key| expected_options.delete key } unless extras.nil? 184 91 185 92 msg = build_message(message, "The recognized options <?> did not match <?>", 186 93 request.path_parameters, expected_options) … … 192 99 # Load routes.rb if it hasn't been loaded. 193 100 ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? 194 101 195 102 # Assume given controller 196 103 request = ActionController::TestRequest.new({}, {}, nil) 197 104 request.path_parameters = (defaults or {}).clone 198 105 request.path_parameters[:controller] ||= options[:controller] 199 106 200 107 generated_path, found_extras = ActionController::Routing::Routes.generate(options, request) 201 108 generated_path = generated_path.join('/') 202 109 msg = build_message(message, "found extras <?>, not <?>", found_extras, extras) 203 110 assert_block(msg) { found_extras == extras } 204 111 205 112 msg = build_message(message, "The generated path <?> did not match <?>", generated_path, 206 113 expected_path) … … 214 121 assert_generates(path, options, defaults, extras, message) 215 122 end 216 217 # -- template assertions ------------------------------------------------ 218 219 # ensure that a template object with the given name exists 220 def assert_template_has(key=nil, message=nil) 221 response = acquire_assertion_target 222 msg = build_message(message, "<?> is not a template object", key ) 223 assert_block(msg) { response.has_template_object?(key) } 224 end 225 226 # ensure that a template object with the given name does not exist 227 def assert_template_has_no(key=nil,message=nil) 228 response = acquire_assertion_target 229 msg = build_message(message, "<?> is a template object <?>", key, response.template_objects[key]) 230 assert_block(msg) { !response.has_template_object?(key) } 231 end 232 233 # ensures that the object assigned to the template on +key+ is equal to +expected+ object. 234 def assert_assigned_equal(expected = nil, key = nil, message = nil) 235 response = acquire_assertion_target 236 msg = build_message(message, "<?> expected in assigns['?'] but was <?>", expected, key, response.template.assigns[key.to_s]) 237 assert_block(msg) { expected == response.template.assigns[key.to_s] } 238 end 239 240 # Asserts that the template returns the +expected+ string or array based on the XPath +expression+. 241 # This will only work if the template rendered a valid XML document. 242 def assert_template_xpath_match(expression=nil, expected=nil, message=nil) 243 response = acquire_assertion_target 244 xml, matches = REXML::Document.new(response.body), [] 245 xml.elements.each(expression) { |e| matches << e.text } 246 if matches.empty? then 247 msg = build_message(message, "<?> not found in document", expression) 248 flunk(msg) 249 return 250 elsif matches.length < 2 then 251 matches = matches.first 252 end 253 254 msg = build_message(message, "<?> found <?>, not <?>", expression, matches, expected) 255 assert_block(msg) { matches == expected } 256 end 257 258 # -- helper functions --------------------------------------------------- 259 260 # get the TestResponse object that these assertions depend upon 261 def acquire_assertion_target 262 target = ActionController::TestResponse.assertion_target 263 assert_block( "Unable to acquire the TestResponse.assertion_target. Please set this before calling this assertion." ) { !target.nil? } 264 target 265 end 266 267 end # Assertions 268 end # Unit 269 end # Test 123 end 124 end 125 end trunk/actionpack/lib/action_controller/flash.rb
r942 r1189 25 25 # See docs on the FlashHash class for more details about the flash. 26 26 module Flash 27 28 27 def self.append_features(base) #:nodoc: 29 28 super … … 45 44 46 45 class FlashHash < Hash 47 48 46 def initialize #:nodoc: 49 47 super … … 114 112 115 113 private 116 117 # Used internally by the <tt>keep</tt> and <tt>discard</tt> methods118 # use() # marks the entire flashas used119 # use('msg') # marks the "msg" entry as used120 # use(nil, false) # marks the entire flashas unused (keeps it around for one more action)121 # use('msg', false) # marks the "msg" entry as unused (keeps it around for one more action)122 def use(k=nil, v=true)123 unless k.nil?124 @used[k] = v125 else126 keys.each{|key| use key, v }114 # Used internally by the <tt>keep</tt> and <tt>discard</tt> methods 115 # use() # marks the entire flash as used 116 # use('msg') # marks the "msg" entry as used 117 # use(nil, false) # marks the entire flash as unused (keeps it around for one more action) 118 # use('msg', false) # marks the "msg" entry as unused (keeps it around for one more action) 119 def use(k=nil, v=true) 120 unless k.nil? 121 @used[k] = v 122 else 123 keys.each{|key| use key, v } 124 end 127 125 end 128 end129 130 126 end 131 127 132 128 133 129 protected 130 # Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or 131 # <tt>flash["notice"] = "hello"</tt> to put a new one. 132 def flash #:doc: 133 @session['flash'] ||= FlashHash.new 134 end 134 135 135 # Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or 136 # <tt>flash["notice"] = "hello"</tt> to put a new one. 137 def flash #:doc: 138 @session['flash'] ||= FlashHash.new 139 end 140 141 # deprecated. use <tt>flash.keep</tt> instead 142 def keep_flash #:doc: 143 flash.keep 144 end 136 # deprecated. use <tt>flash.keep</tt> instead 137 def keep_flash #:doc: 138 flash.keep 139 end 145 140 146 141 147 private142 private 148 143 149 # marks flash entries as used and expose the flash to the view150 def fire_flash151 flash.discard152 @assigns["flash"] = flash153 end144 # marks flash entries as used and expose the flash to the view 145 def fire_flash 146 flash.discard 147 @assigns["flash"] = flash 148 end 154 149 155 # deletes the flash entries that were not marked for keeping 156 def sweep_flash 157 flash.sweep 158 end 159 150 # deletes the flash entries that were not marked for keeping 151 def sweep_flash 152 flash.sweep 153 end 160 154 end 161 155 end trunk/actionpack/lib/action_controller/test_process.rb
r1175 r1189 1 require File.dirname(__FILE__) + '/assertions /action_pack_assertions'2 require File.dirname(__FILE__) + '/ assertions/active_record_assertions'1 require File.dirname(__FILE__) + '/assertions' 2 require File.dirname(__FILE__) + '/deprecated_assertions' 3 3 4 4 if defined?(RAILS_ROOT) … … 95 95 96 96 class TestResponse < AbstractResponse #:nodoc: 97 # the class attribute ties a TestResponse to the assertions98 class << self99 attr_accessor :assertion_target100 end101 102 # initializer103 def initialize104 TestResponse.assertion_target=self# if TestResponse.assertion_target.nil?105 super()106 end107 108 97 # the response code of the request 109 98 def response_code … … 127 116 128 117 # was there a server-side error? 129 def server_error?118 def error? 130 119 (500..599).include?(response_code) 131 120 end 121 122 alias_method :server_error?, :error? 132 123 133 124 # returns the redirection location or nil … … 282 273 end 283 274 284 def assigns(name) 285 @response.template.assigns[name.to_s] 275 def assigns(key = nil) 276 if key.nil? 277 @response.template.assigns 278 else 279 @response.template.assigns[key.to_s] 280 end 281 end 282 283 def session 284 @response.session 285 end 286 287 def flash 288 @response.flash 289 end 290 291 def cookies 292 @response.cookies 293 end 294 295 def redirect_to_url 296 @response.redirect_url 286 297 end 287 298 trunk/actionpack/lib/action_view/base.rb
r1169 r1189 120 120 include ERB::Util 121 121 122 attr_reader :first_render122 attr_reader :first_render 123 123 attr_accessor :base_path, :assigns, :template_extension 124 124 attr_accessor :controller … … 158 158 159 159 def initialize(base_path = nil, assigns_for_first_render = {}, controller = nil)#:nodoc: 160 @base_path, @assigns = base_path, assigns_for_first_render 160 @base_path, @assigns = base_path, assigns_for_first_render.with_indifferent_access 161 161 @controller = controller 162 162 end trunk/actionpack/test/controller/action_pack_assertions_test.rb
r942 r1189 171 171 assert @response.has_flash_with_contents? 172 172 assert_flash_exists 173 assert ActionController::TestResponse.assertion_target.has_flash_with_contents?174 173 assert_flash_not_empty 175 174 assert_flash_has 'hello'