aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-04-17 11:38:01 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-04-17 11:38:01 +0000
commitdca7efa67ec12b986ed03844af588055c9d8bdb9 (patch)
tree13207b1dce3af68528ddfe11d32ee98f627eb0a0 /actionpack/lib
parentd5b67ed8d33dbde44d24d7d93135261062e550d1 (diff)
downloadrails-dca7efa67ec12b986ed03844af588055c9d8bdb9.tar.gz
rails-dca7efa67ec12b986ed03844af588055c9d8bdb9.tar.bz2
rails-dca7efa67ec12b986ed03844af588055c9d8bdb9.zip
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.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1189 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_controller/assertions.rb125
-rw-r--r--actionpack/lib/action_controller/assertions/action_pack_assertions.rb269
-rw-r--r--actionpack/lib/action_controller/assertions/active_record_assertions.rb65
-rw-r--r--actionpack/lib/action_controller/deprecated_assertions.rb200
-rw-r--r--actionpack/lib/action_controller/flash.rb66
-rw-r--r--actionpack/lib/action_controller/test_process.rb43
-rw-r--r--actionpack/lib/action_view/base.rb4
7 files changed, 384 insertions, 388 deletions
diff --git a/actionpack/lib/action_controller/assertions.rb b/actionpack/lib/action_controller/assertions.rb
new file mode 100644
index 0000000000..ff6d88fcb2
--- /dev/null
+++ b/actionpack/lib/action_controller/assertions.rb
@@ -0,0 +1,125 @@
+require 'test/unit'
+require 'test/unit/assertions'
+require 'rexml/document'
+
+module Test #:nodoc:
+ module Unit #:nodoc:
+ # In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions
+ # can be used against. These collections are:
+ #
+ # * assigns: Instance variables assigned in the action that's available for the view.
+ # * session: Objects being saved in the session.
+ # * flash: The flash objects being currently in the session.
+ # * cookies: Cookies being sent to the user on this request.
+ #
+ # These collections can be used just like any other hash:
+ #
+ # assert_not_nil assigns[:person] # makes sure that a @person instance variable was set
+ # assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave"
+ # assert flash.empty? # makes sure that there's nothing in the flash
+ #
+ # On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url.
+ #
+ # For redirects within the same controller, you can even call follow_redirect and the redirect will be follow triggering another
+ # action call which can then be asserted against.
+ module Assertions
+ # Asserts that the response is one of the following types:
+ #
+ # * <tt>:success</tt>: Status code was 200
+ # * <tt>:redirect</tt>: Status code was in the 300-399 range
+ # * <tt>:missing</tt>: Status code was 404
+ # * <tt>:error</tt>: Status code was in the 500-599 range
+ #
+ # You can also pass an explicit status code number as the type, like assert_response(501)
+ def assert_response(type, message = nil)
+ 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
+ else
+ assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
+ 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 at 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)
+ assert_redirect(message)
+
+ msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>)", @response.redirected_to)
+ assert_block(msg) do
+ if options.is_a?(Symbol)
+ @response.redirected_to == options
+ else
+ options.keys.all? do |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])
+ end
+ end
+ end
+ end
+
+ # Asserts that the request was rendered with the appropriate template file
+ def assert_template(expected=nil, message=nil)
+ rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file
+ msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered)
+ assert_block(msg) do
+ if expected.nil?
+ @response.rendered_with_file?
+ else
+ expected == rendered
+ end
+ end
+ end
+
+ alias_method :assert_rendered_file, :assert_template #:nodoc:
+
+ # -- routing assertions --------------------------------------------------
+
+ # Asserts that the routing of the given path is handled correctly and that the parsed options match.
+ def assert_recognizes(expected_options, path, extras={}, message=nil)
+ # Load routes.rb if it hasn't been loaded.
+ ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
+
+ # Assume given controller
+ request = ActionController::TestRequest.new({}, {}, nil)
+ request.path = path
+ ActionController::Routing::Routes.recognize!(request)
+
+ expected_options = expected_options.clone
+ extras.each_key { |key| expected_options.delete key } unless extras.nil?
+
+ msg = build_message(message, "The recognized options <?> did not match <?>",
+ request.path_parameters, expected_options)
+ assert_block(msg) { request.path_parameters == expected_options }
+ end
+
+ # Asserts that the provided options can be used to generate the provided path.
+ def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
+ # Load routes.rb if it hasn't been loaded.
+ ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
+
+ # Assume given controller
+ request = ActionController::TestRequest.new({}, {}, nil)
+ request.path_parameters = (defaults or {}).clone
+ request.path_parameters[:controller] ||= options[:controller]
+
+ generated_path, found_extras = ActionController::Routing::Routes.generate(options, request)
+ generated_path = generated_path.join('/')
+ msg = build_message(message, "found extras <?>, not <?>", found_extras, extras)
+ assert_block(msg) { found_extras == extras }
+
+ msg = build_message(message, "The generated path <?> did not match <?>", generated_path,
+ expected_path)
+ assert_block(msg) { expected_path == generated_path }
+ end
+
+ # asserts that path and options match both ways, in other words, the URL generated from
+ # options is same as path, and also that the options recognized from path are same as options
+ def assert_routing(path, options, defaults={}, extras={}, message=nil)
+ assert_recognizes(options, path, extras, message)
+ assert_generates(path, options, defaults, extras, message)
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/actionpack/lib/action_controller/assertions/action_pack_assertions.rb b/actionpack/lib/action_controller/assertions/action_pack_assertions.rb
deleted file mode 100644
index 9bbcc5b83f..0000000000
--- a/actionpack/lib/action_controller/assertions/action_pack_assertions.rb
+++ /dev/null
@@ -1,269 +0,0 @@
-require 'test/unit'
-require 'test/unit/assertions'
-require 'rexml/document'
-
-module Test #:nodoc:
- module Unit #:nodoc:
- # Adds a wealth of assertions to do functional testing of Action Controllers.
- module Assertions
- # -- basic assertions ---------------------------------------------------
-
- # ensure that the web request has been serviced correctly
- def assert_success(message=nil)
- response = acquire_assertion_target
- if response.success?
- # to count the assertion
- assert_block("") { true }
- else
- if response.redirect?
- msg = build_message(message, "Response unexpectedly redirect to <?>", response.redirect_url)
- else
- msg = build_message(message, "unsuccessful request (response code = <?>)",
- response.response_code)
- end
- assert_block(msg) { false }
- end
- end
-
- # ensure the request was rendered with the appropriate template file
- def assert_rendered_file(expected=nil, message=nil)
- response = acquire_assertion_target
- rendered = expected ? response.rendered_file(!expected.include?('/')) : response.rendered_file
- msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered)
- assert_block(msg) do
- if expected.nil?
- response.rendered_with_file?
- else
- expected == rendered
- end
- end
- end
-
- # -- session assertions -------------------------------------------------
-
- # ensure that the session has an object with the specified name
- def assert_session_has(key=nil, message=nil)
- response = acquire_assertion_target
- msg = build_message(message, "<?> is not in the session <?>", key, response.session)
- assert_block(msg) { response.has_session_object?(key) }
- end
-
- # ensure that the session has no object with the specified name
- def assert_session_has_no(key=nil, message=nil)
- response = acquire_assertion_target
- msg = build_message(message, "<?> is in the session <?>", key, response.session)
- assert_block(msg) { !response.has_session_object?(key) }
- end
-
- def assert_session_equal(expected = nil, key = nil, message = nil)
- response = acquire_assertion_target
- msg = build_message(message, "<?> expected in session['?'] but was <?>", expected, key, response.session[key])
- assert_block(msg) { expected == response.session[key] }
- end
-
- # -- cookie assertions ---------------------------------------------------
-
- def assert_no_cookie(key = nil, message = nil)
- response = acquire_assertion_target
- actual = response.cookies[key]
- msg = build_message(message, "<?> not expected in cookies['?']", actual, key)
- assert_block(msg) { actual.nil? or actual.empty? }
- end
-
- def assert_cookie_equal(expected = nil, key = nil, message = nil)
- response = acquire_assertion_target
- actual = response.cookies[key]
- actual = actual.first if actual
- msg = build_message(message, "<?> expected in cookies['?'] but was <?>", expected, key, actual)
- assert_block(msg) { expected == actual }
- end
-
- # -- flash assertions ---------------------------------------------------
-
- # ensure that the flash has an object with the specified name
- def assert_flash_has(key=nil, message=nil)
- response = acquire_assertion_target
- msg = build_message(message, "<?> is not in the flash <?>", key, response.flash)
- assert_block(msg) { response.has_flash_object?(key) }
- end
-
- # ensure that the flash has no object with the specified name
- def assert_flash_has_no(key=nil, message=nil)
- response = acquire_assertion_target
- msg = build_message(message, "<?> is in the flash <?>", key, response.flash)
- assert_block(msg) { !response.has_flash_object?(key) }
- end
-
- # ensure the flash exists
- def assert_flash_exists(message=nil)
- response = acquire_assertion_target
- msg = build_message(message, "the flash does not exist <?>", response.session['flash'] )
- assert_block(msg) { response.has_flash? }
- end
-
- # ensure the flash does not exist
- def assert_flash_not_exists(message=nil)
- response = acquire_assertion_target
- msg = build_message(message, "the flash exists <?>", response.flash)
- assert_block(msg) { !response.has_flash? }
- end
-
- # ensure the flash is empty but existent
- def assert_flash_empty(message=nil)
- response = acquire_assertion_target
- msg = build_message(message, "the flash is not empty <?>", response.flash)
- assert_block(msg) { !response.has_flash_with_contents? }
- end
-
- # ensure the flash is not empty
- def assert_flash_not_empty(message=nil)
- response = acquire_assertion_target
- msg = build_message(message, "the flash is empty")
- assert_block(msg) { response.has_flash_with_contents? }
- end
-
- def assert_flash_equal(expected = nil, key = nil, message = nil)
- response = acquire_assertion_target
- msg = build_message(message, "<?> expected in flash['?'] but was <?>", expected, key, response.flash[key])
- assert_block(msg) { expected == response.flash[key] }
- end
-
- # -- redirection assertions ---------------------------------------------
-
- # ensure we have be redirected
- def assert_redirect(message=nil)
- response = acquire_assertion_target
- msg = build_message(message, "response is not a redirection (response code is <?>)", response.response_code)
- assert_block(msg) { response.redirect? }
- end
-
- def assert_redirected_to(options = {}, message=nil)
- assert_redirect(message)
- response = acquire_assertion_target
-
- msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>)", response.redirected_to)
- assert_block(msg) do
- if options.is_a?(Symbol)
- response.redirected_to == options
- else
- 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] ) }
- end
- end
- end
-
- # ensure our redirection url is an exact match
- def assert_redirect_url(url=nil, message=nil)
- assert_redirect(message)
- response = acquire_assertion_target
- msg = build_message(message, "<?> is not the redirected location <?>", url, response.redirect_url)
- assert_block(msg) { response.redirect_url == url }
- end
-
- # ensure our redirection url matches a pattern
- def assert_redirect_url_match(pattern=nil, message=nil)
- assert_redirect(message)
- response = acquire_assertion_target
- msg = build_message(message, "<?> was not found in the location: <?>", pattern, response.redirect_url)
- assert_block(msg) { response.redirect_url_match?(pattern) }
- end
-
- # -- routing assertions --------------------------------------------------
-
- # Asserts that the routing of the given path is handled correctly and that the parsed options match.
- def assert_recognizes(expected_options, path, extras={}, message=nil)
- # Load routes.rb if it hasn't been loaded.
- ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
-
- # Assume given controller
- request = ActionController::TestRequest.new({}, {}, nil)
- request.path = path
- ActionController::Routing::Routes.recognize!(request)
-
- expected_options = expected_options.clone
- extras.each_key { |key| expected_options.delete key } unless extras.nil?
-
- msg = build_message(message, "The recognized options <?> did not match <?>",
- request.path_parameters, expected_options)
- assert_block(msg) { request.path_parameters == expected_options }
- end
-
- # Asserts that the provided options can be used to generate the provided path.
- def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
- # Load routes.rb if it hasn't been loaded.
- ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
-
- # Assume given controller
- request = ActionController::TestRequest.new({}, {}, nil)
- request.path_parameters = (defaults or {}).clone
- request.path_parameters[:controller] ||= options[:controller]
-
- generated_path, found_extras = ActionController::Routing::Routes.generate(options, request)
- generated_path = generated_path.join('/')
- msg = build_message(message, "found extras <?>, not <?>", found_extras, extras)
- assert_block(msg) { found_extras == extras }
-
- msg = build_message(message, "The generated path <?> did not match <?>", generated_path,
- expected_path)
- assert_block(msg) { expected_path == generated_path }
- end
-
- # asserts that path and options match both ways, in other words, the URL generated from
- # options is same as path, and also that the options recognized from path are same as options
- def assert_routing(path, options, defaults={}, extras={}, message=nil)
- assert_recognizes(options, path, extras, message)
- assert_generates(path, options, defaults, extras, message)
- end
-
- # -- template assertions ------------------------------------------------
-
- # ensure that a template object with the given name exists
- def assert_template_has(key=nil, message=nil)
- response = acquire_assertion_target
- msg = build_message(message, "<?> is not a template object", key )
- assert_block(msg) { response.has_template_object?(key) }
- end
-
- # ensure that a template object with the given name does not exist
- def assert_template_has_no(key=nil,message=nil)
- response = acquire_assertion_target
- msg = build_message(message, "<?> is a template object <?>", key, response.template_objects[key])
- assert_block(msg) { !response.has_template_object?(key) }
- end
-
- # ensures that the object assigned to the template on +key+ is equal to +expected+ object.
- def assert_assigned_equal(expected = nil, key = nil, message = nil)
- response = acquire_assertion_target
- msg = build_message(message, "<?> expected in assigns['?'] but was <?>", expected, key, response.template.assigns[key.to_s])
- assert_block(msg) { expected == response.template.assigns[key.to_s] }
- end
-
- # Asserts that the template returns the +expected+ string or array based on the XPath +expression+.
- # This will only work if the template rendered a valid XML document.
- def assert_template_xpath_match(expression=nil, expected=nil, message=nil)
- response = acquire_assertion_target
- xml, matches = REXML::Document.new(response.body), []
- xml.elements.each(expression) { |e| matches << e.text }
- if matches.empty? then
- msg = build_message(message, "<?> not found in document", expression)
- flunk(msg)
- return
- elsif matches.length < 2 then
- matches = matches.first
- end
-
- msg = build_message(message, "<?> found <?>, not <?>", expression, matches, expected)
- assert_block(msg) { matches == expected }
- end
-
- # -- helper functions ---------------------------------------------------
-
- # get the TestResponse object that these assertions depend upon
- def acquire_assertion_target
- target = ActionController::TestResponse.assertion_target
- assert_block( "Unable to acquire the TestResponse.assertion_target. Please set this before calling this assertion." ) { !target.nil? }
- target
- end
-
- end # Assertions
- end # Unit
-end # Test
diff --git a/actionpack/lib/action_controller/assertions/active_record_assertions.rb b/actionpack/lib/action_controller/assertions/active_record_assertions.rb
deleted file mode 100644
index cdc9b6951d..0000000000
--- a/actionpack/lib/action_controller/assertions/active_record_assertions.rb
+++ /dev/null
@@ -1,65 +0,0 @@
-require 'test/unit'
-require 'test/unit/assertions'
-# active_record is assumed to be loaded by this point
-
-module Test #:nodoc:
- module Unit #:nodoc:
- module Assertions
- # Assert the template object with the given name is an Active Record descendant and is valid.
- def assert_valid_record(key = nil, message = nil)
- record = find_record_in_template(key)
- msg = build_message(message, "Active Record is invalid <?>)", record.errors.full_messages)
- assert_block(msg) { record.valid? }
- end
-
- # Assert the template object with the given name is an Active Record descendant and is invalid.
- def assert_invalid_record(key = nil, message = nil)
- record = find_record_in_template(key)
- msg = build_message(message, "Active Record is valid)")
- assert_block(msg) { !record.valid? }
- end
-
- # Assert the template object with the given name is an Active Record descendant and the specified column(s) are valid.
- def assert_valid_column_on_record(key = nil, columns = "", message = nil)
- record = find_record_in_template(key)
- record.send(:validate)
-
- cols = glue_columns(columns)
- cols.delete_if { |col| !record.errors.invalid?(col) }
- msg = build_message(message, "Active Record has invalid columns <?>)", cols.join(",") )
- assert_block(msg) { cols.empty? }
- end
-
- # Assert the template object with the given name is an Active Record descendant and the specified column(s) are invalid.
- def assert_invalid_column_on_record(key = nil, columns = "", message = nil)
- record = find_record_in_template(key)
- record.send(:validate)
-
- cols = glue_columns(columns)
- cols.delete_if { |col| record.errors.invalid?(col) }
- msg = build_message(message, "Active Record has valid columns <?>)", cols.join(",") )
- assert_block(msg) { cols.empty? }
- end
-
- private
- def glue_columns(columns)
- cols = []
- cols << columns if columns.class == String
- cols += columns if columns.class == Array
- cols
- end
-
- def find_record_in_template(key = nil)
- response = acquire_assertion_target
-
- assert_template_has(key)
- record = response.template_objects[key]
-
- assert_not_nil(record)
- assert_kind_of ActiveRecord::Base, record
-
- return record
- end
- end
- end
-end \ No newline at end of file
diff --git a/actionpack/lib/action_controller/deprecated_assertions.rb b/actionpack/lib/action_controller/deprecated_assertions.rb
new file mode 100644
index 0000000000..37505d571c
--- /dev/null
+++ b/actionpack/lib/action_controller/deprecated_assertions.rb
@@ -0,0 +1,200 @@
+require 'test/unit'
+require 'test/unit/assertions'
+require 'rexml/document'
+
+module Test #:nodoc:
+ module Unit #:nodoc:
+ module Assertions #:nodoc:
+ def assert_success(message=nil)
+ assert_response(:success, message)
+ end
+
+ def assert_redirect(message=nil)
+ assert_response(:redirect, message)
+ end
+
+ # ensure that the session has an object with the specified name
+ def assert_session_has(key=nil, message=nil)
+ msg = build_message(message, "<?> is not in the session <?>", key, @response.session)
+ assert_block(msg) { @response.has_session_object?(key) }
+ end
+
+ # ensure that the session has no object with the specified name
+ def assert_session_has_no(key=nil, message=nil)
+ msg = build_message(message, "<?> is in the session <?>", key, @response.session)
+ assert_block(msg) { !@response.has_session_object?(key) }
+ end
+
+ def assert_session_equal(expected = nil, key = nil, message = nil)
+ msg = build_message(message, "<?> expected in session['?'] but was <?>", expected, key, @response.session[key])
+ assert_block(msg) { expected == @response.session[key] }
+ end
+
+ # -- cookie assertions ---------------------------------------------------
+
+ def assert_no_cookie(key = nil, message = nil)
+ actual = @response.cookies[key]
+ msg = build_message(message, "<?> not expected in cookies['?']", actual, key)
+ assert_block(msg) { actual.nil? or actual.empty? }
+ end
+
+ def assert_cookie_equal(expected = nil, key = nil, message = nil)
+ actual = @response.cookies[key]
+ actual = actual.first if actual
+ msg = build_message(message, "<?> expected in cookies['?'] but was <?>", expected, key, actual)
+ assert_block(msg) { expected == actual }
+ end
+
+ # -- flash assertions ---------------------------------------------------
+
+ # ensure that the flash has an object with the specified name
+ def assert_flash_has(key=nil, message=nil)
+ msg = build_message(message, "<?> is not in the flash <?>", key, @response.flash)
+ assert_block(msg) { @response.has_flash_object?(key) }
+ end
+
+ # ensure that the flash has no object with the specified name
+ def assert_flash_has_no(key=nil, message=nil)
+ msg = build_message(message, "<?> is in the flash <?>", key, @response.flash)
+ assert_block(msg) { !@response.has_flash_object?(key) }
+ end
+
+ # ensure the flash exists
+ def assert_flash_exists(message=nil)
+ msg = build_message(message, "the flash does not exist <?>", @response.session['flash'] )
+ assert_block(msg) { @response.has_flash? }
+ end
+
+ # ensure the flash does not exist
+ def assert_flash_not_exists(message=nil)
+ msg = build_message(message, "the flash exists <?>", @response.flash)
+ assert_block(msg) { !@response.has_flash? }
+ end
+
+ # ensure the flash is empty but existent
+ def assert_flash_empty(message=nil)
+ msg = build_message(message, "the flash is not empty <?>", @response.flash)
+ assert_block(msg) { !@response.has_flash_with_contents? }
+ end
+
+ # ensure the flash is not empty
+ def assert_flash_not_empty(message=nil)
+ msg = build_message(message, "the flash is empty")
+ assert_block(msg) { @response.has_flash_with_contents? }
+ end
+
+ def assert_flash_equal(expected = nil, key = nil, message = nil)
+ msg = build_message(message, "<?> expected in flash['?'] but was <?>", expected, key, @response.flash[key])
+ assert_block(msg) { expected == @response.flash[key] }
+ end
+
+
+ # ensure our redirection url is an exact match
+ def assert_redirect_url(url=nil, message=nil)
+ assert_redirect(message)
+ msg = build_message(message, "<?> is not the redirected location <?>", url, @response.redirect_url)
+ assert_block(msg) { @response.redirect_url == url }
+ end
+
+ # ensure our redirection url matches a pattern
+ def assert_redirect_url_match(pattern=nil, message=nil)
+ assert_redirect(message)
+ msg = build_message(message, "<?> was not found in the location: <?>", pattern, @response.redirect_url)
+ assert_block(msg) { @response.redirect_url_match?(pattern) }
+ end
+
+
+ # -- template assertions ------------------------------------------------
+
+ # ensure that a template object with the given name exists
+ def assert_template_has(key=nil, message=nil)
+ msg = build_message(message, "<?> is not a template object", key )
+ assert_block(msg) { @response.has_template_object?(key) }
+ end
+
+ # ensure that a template object with the given name does not exist
+ def assert_template_has_no(key=nil,message=nil)
+ msg = build_message(message, "<?> is a template object <?>", key, @response.template_objects[key])
+ assert_block(msg) { !@response.has_template_object?(key) }
+ end
+
+ # ensures that the object assigned to the template on +key+ is equal to +expected+ object.
+ def assert_template_equal(expected = nil, key = nil, message = nil)
+ msg = build_message(message, "<?> expected in assigns['?'] but was <?>", expected, key, @response.template.assigns[key.to_s])
+ assert_block(msg) { expected == @response.template.assigns[key.to_s] }
+ end
+ alias_method :assert_assigned_equal, :assert_template_equal
+
+ # Asserts that the template returns the +expected+ string or array based on the XPath +expression+.
+ # This will only work if the template rendered a valid XML document.
+ def assert_template_xpath_match(expression=nil, expected=nil, message=nil)
+ xml, matches = REXML::Document.new(@response.body), []
+ xml.elements.each(expression) { |e| matches << e.text }
+ if matches.empty? then
+ msg = build_message(message, "<?> not found in document", expression)
+ flunk(msg)
+ return
+ elsif matches.length < 2 then
+ matches = matches.first
+ end
+
+ msg = build_message(message, "<?> found <?>, not <?>", expression, matches, expected)
+ assert_block(msg) { matches == expected }
+ end
+
+ # Assert the template object with the given name is an Active Record descendant and is valid.
+ def assert_valid_record(key = nil, message = nil)
+ record = find_record_in_template(key)
+ msg = build_message(message, "Active Record is invalid <?>)", record.errors.full_messages)
+ assert_block(msg) { record.valid? }
+ end
+
+ # Assert the template object with the given name is an Active Record descendant and is invalid.
+ def assert_invalid_record(key = nil, message = nil)
+ record = find_record_in_template(key)
+ msg = build_message(message, "Active Record is valid)")
+ assert_block(msg) { !record.valid? }
+ end
+
+ # Assert the template object with the given name is an Active Record descendant and the specified column(s) are valid.
+ def assert_valid_column_on_record(key = nil, columns = "", message = nil)
+ record = find_record_in_template(key)
+ record.send(:validate)
+
+ cols = glue_columns(columns)
+ cols.delete_if { |col| !record.errors.invalid?(col) }
+ msg = build_message(message, "Active Record has invalid columns <?>)", cols.join(",") )
+ assert_block(msg) { cols.empty? }
+ end
+
+ # Assert the template object with the given name is an Active Record descendant and the specified column(s) are invalid.
+ def assert_invalid_column_on_record(key = nil, columns = "", message = nil)
+ record = find_record_in_template(key)
+ record.send(:validate)
+
+ cols = glue_columns(columns)
+ cols.delete_if { |col| record.errors.invalid?(col) }
+ msg = build_message(message, "Active Record has valid columns <?>)", cols.join(",") )
+ assert_block(msg) { cols.empty? }
+ end
+
+ private
+ def glue_columns(columns)
+ cols = []
+ cols << columns if columns.class == String
+ cols += columns if columns.class == Array
+ cols
+ end
+
+ def find_record_in_template(key = nil)
+ assert_template_has(key)
+ record = @response.template_objects[key]
+
+ assert_not_nil(record)
+ assert_kind_of ActiveRecord::Base, record
+
+ return record
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/actionpack/lib/action_controller/flash.rb b/actionpack/lib/action_controller/flash.rb
index e3a8a8c8c4..90c464dd81 100644
--- a/actionpack/lib/action_controller/flash.rb
+++ b/actionpack/lib/action_controller/flash.rb
@@ -24,7 +24,6 @@ module ActionController #:nodoc:
#
# See docs on the FlashHash class for more details about the flash.
module Flash
-
def self.append_features(base) #:nodoc:
super
base.before_filter(:fire_flash)
@@ -44,7 +43,6 @@ module ActionController #:nodoc:
end
class FlashHash < Hash
-
def initialize #:nodoc:
super
@used = {}
@@ -113,49 +111,45 @@ module ActionController #:nodoc:
end
private
-
- # Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
- # use() # marks the entire flash as used
- # use('msg') # marks the "msg" entry as used
- # use(nil, false) # marks the entire flash as unused (keeps it around for one more action)
- # use('msg', false) # marks the "msg" entry as unused (keeps it around for one more action)
- def use(k=nil, v=true)
- unless k.nil?
- @used[k] = v
- else
- keys.each{|key| use key, v }
+ # Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
+ # use() # marks the entire flash as used
+ # use('msg') # marks the "msg" entry as used
+ # use(nil, false) # marks the entire flash as unused (keeps it around for one more action)
+ # use('msg', false) # marks the "msg" entry as unused (keeps it around for one more action)
+ def use(k=nil, v=true)
+ unless k.nil?
+ @used[k] = v
+ else
+ keys.each{|key| use key, v }
+ end
end
- end
-
end
protected
+ # Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or
+ # <tt>flash["notice"] = "hello"</tt> to put a new one.
+ def flash #:doc:
+ @session['flash'] ||= FlashHash.new
+ end
- # Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or
- # <tt>flash["notice"] = "hello"</tt> to put a new one.
- def flash #:doc:
- @session['flash'] ||= FlashHash.new
- end
-
- # deprecated. use <tt>flash.keep</tt> instead
- def keep_flash #:doc:
- flash.keep
- end
-
+ # deprecated. use <tt>flash.keep</tt> instead
+ def keep_flash #:doc:
+ flash.keep
+ end
- private
- # marks flash entries as used and expose the flash to the view
- def fire_flash
- flash.discard
- @assigns["flash"] = flash
- end
+ private
- # deletes the flash entries that were not marked for keeping
- def sweep_flash
- flash.sweep
- end
+ # marks flash entries as used and expose the flash to the view
+ def fire_flash
+ flash.discard
+ @assigns["flash"] = flash
+ end
+ # deletes the flash entries that were not marked for keeping
+ def sweep_flash
+ flash.sweep
+ end
end
end
diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb
index 6931e7cdcb..83cde64b85 100644
--- a/actionpack/lib/action_controller/test_process.rb
+++ b/actionpack/lib/action_controller/test_process.rb
@@ -1,5 +1,5 @@
-require File.dirname(__FILE__) + '/assertions/action_pack_assertions'
-require File.dirname(__FILE__) + '/assertions/active_record_assertions'
+require File.dirname(__FILE__) + '/assertions'
+require File.dirname(__FILE__) + '/deprecated_assertions'
if defined?(RAILS_ROOT)
# Temporary hack for getting functional tests in Rails running under 1.8.2
@@ -94,17 +94,6 @@ module ActionController #:nodoc:
end
class TestResponse < AbstractResponse #:nodoc:
- # the class attribute ties a TestResponse to the assertions
- class << self
- attr_accessor :assertion_target
- end
-
- # initializer
- def initialize
- TestResponse.assertion_target=self# if TestResponse.assertion_target.nil?
- super()
- end
-
# the response code of the request
def response_code
headers['Status'][0,3].to_i rescue 0
@@ -126,10 +115,12 @@ module ActionController #:nodoc:
end
# was there a server-side error?
- def server_error?
+ def error?
(500..599).include?(response_code)
end
+ alias_method :server_error?, :error?
+
# returns the redirection location or nil
def redirect_url
redirect? ? headers['location'] : nil
@@ -281,8 +272,28 @@ module Test
get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys)
end
- def assigns(name)
- @response.template.assigns[name.to_s]
+ def assigns(key = nil)
+ if key.nil?
+ @response.template.assigns
+ else
+ @response.template.assigns[key.to_s]
+ end
+ end
+
+ def session
+ @response.session
+ end
+
+ def flash
+ @response.flash
+ end
+
+ def cookies
+ @response.cookies
+ end
+
+ def redirect_to_url
+ @response.redirect_url
end
def build_request_uri(action, parameters)
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index 2c906b90ba..cffbbfcb6b 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -119,7 +119,7 @@ module ActionView #:nodoc:
class Base
include ERB::Util
- attr_reader :first_render
+ attr_reader :first_render
attr_accessor :base_path, :assigns, :template_extension
attr_accessor :controller
@@ -157,7 +157,7 @@ module ActionView #:nodoc:
end
def initialize(base_path = nil, assigns_for_first_render = {}, controller = nil)#:nodoc:
- @base_path, @assigns = base_path, assigns_for_first_render
+ @base_path, @assigns = base_path, assigns_for_first_render.with_indifferent_access
@controller = controller
end