aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorMarcel Molina <marcel@vernix.org>2007-12-05 18:17:23 +0000
committerMarcel Molina <marcel@vernix.org>2007-12-05 18:17:23 +0000
commitc27b9db39fd1d86448572c06df2bd097debbfe10 (patch)
treeaf8b258f16e437d766cd2520f3e768e8ef9e5e32 /actionpack
parentcc0c4202be8a09b1d26adf12495b7f1b24957279 (diff)
downloadrails-c27b9db39fd1d86448572c06df2bd097debbfe10.tar.gz
rails-c27b9db39fd1d86448572c06df2bd097debbfe10.tar.bz2
rails-c27b9db39fd1d86448572c06df2bd097debbfe10.zip
Add many examples to assertion documentation. Closes #7803 [jeremymcanally]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8300 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_controller/assertions/response_assertions.rb1
-rw-r--r--actionpack/lib/action_controller/assertions/routing_assertions.rb68
-rw-r--r--actionpack/lib/action_controller/assertions/selector_assertions.rb61
-rw-r--r--actionpack/lib/action_controller/assertions/tag_assertions.rb35
5 files changed, 130 insertions, 37 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index c13ac570f6..4338efc161 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add many examples to assertion documentation. Closes #7803 [jeremymcanally]
+
* Document the supported options for sortable_element. Closes #8820 [berkelep]
* Add examples in the documentation for various assertions. Closes #9938 [zapnap]
diff --git a/actionpack/lib/action_controller/assertions/response_assertions.rb b/actionpack/lib/action_controller/assertions/response_assertions.rb
index 1ac0f234d4..42bd7fb3d2 100644
--- a/actionpack/lib/action_controller/assertions/response_assertions.rb
+++ b/actionpack/lib/action_controller/assertions/response_assertions.rb
@@ -3,6 +3,7 @@ require 'html/document'
module ActionController
module Assertions
+ # A small suite of assertions that test responses from Rails applications.
module ResponseAssertions
# Asserts that the response is one of the following types:
#
diff --git a/actionpack/lib/action_controller/assertions/routing_assertions.rb b/actionpack/lib/action_controller/assertions/routing_assertions.rb
index 6cd4d48b9e..9bff283245 100644
--- a/actionpack/lib/action_controller/assertions/routing_assertions.rb
+++ b/actionpack/lib/action_controller/assertions/routing_assertions.rb
@@ -1,25 +1,41 @@
module ActionController
module Assertions
+ # Suite of assertions to test routes generated by Rails and the handling of requests made to them.
module RoutingAssertions
- # Asserts that the routing of the given path was handled correctly and that the parsed options match.
+ # Asserts that the routing of the given +path+ was handled correctly and that the parsed options (given in the +expected_options+ hash)
+ # match +path+. Basically, it asserts that Rails recognizes the route given by +expected_options+.
#
- # assert_recognizes({:controller => 'items', :action => 'index'}, 'items') # check the default action
- # assert_recognizes({:controller => 'items', :action => 'list'}, 'items/list') # check a specific action
- # assert_recognizes({:controller => 'items', :action => 'list', :id => '1'}, 'items/list/1') # check an action with a parameter
- #
- # Pass a hash in the second argument to specify the request method. This is useful for routes
+ # Pass a hash in the second argument (+path+) to specify the request method. This is useful for routes
# requiring a specific HTTP method. The hash should contain a :path with the incoming request path
# and a :method containing the required HTTP verb.
#
# # assert that POSTing to /items will call the create action on ItemsController
# assert_recognizes({:controller => 'items', :action => 'create'}, {:path => 'items', :method => :post})
#
- # You can also pass in "extras" with a hash containing URL parameters that would normally be in the query string. This can be used
+ # You can also pass in +extras+ with a hash containing URL parameters that would normally be in the query string. This can be used
# to assert that values in the query string string will end up in the params hash correctly. To test query strings you must use the
# extras argument, appending the query string on the path directly will not work. For example:
#
# # assert that a path of '/items/list/1?view=print' returns the correct options
# assert_recognizes({:controller => 'items', :action => 'list', :id => '1', :view => 'print'}, 'items/list/1', { :view => "print" })
+ #
+ # The +message+ parameter allows you to pass in an error message that is displayed upon failure.
+ #
+ # ==== Examples
+ # # Check the default route (i.e., the index action)
+ # assert_recognizes({:controller => 'items', :action => 'index'}, 'items')
+ #
+ # # Test a specific action
+ # assert_recognizes({:controller => 'items', :action => 'list'}, 'items/list')
+ #
+ # # Test an action with a parameter
+ # assert_recognizes({:controller => 'items', :action => 'destroy', :id => '1'}, 'items/destroy/1')
+ #
+ # # Test a custom route
+ # assert_recognizes({:controller => 'items', :action => 'show', :id => '1'}, 'view/item1')
+ #
+ # # Check a Simply RESTful generated route
+ # assert_recognizes(list_items_url, 'items/list')
def assert_recognizes(expected_options, path, extras={}, message=nil)
if path.is_a? Hash
request_method = path[:method]
@@ -43,12 +59,24 @@ module ActionController
end
end
- # Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes.
- # For example:
+ # Asserts that the provided options can be used to generate the provided path. This is the inverse of #assert_recognizes.
+ # The +extras+ parameter is used to tell the request the names and values of additional request parameters that would be in
+ # a query string. The +message+ parameter allows you to specify a custom error message for assertion failures.
#
+ # The +defaults+ parameter is unused.
+ #
+ # ==== Examples
+ # # Asserts that the default action is generated for a route with no action
# assert_generates("/items", :controller => "items", :action => "index")
+ #
+ # # Tests that the list action is properly routed
# assert_generates("/items/list", :controller => "items", :action => "list")
+ #
+ # # Tests the generation of a route with a parameter
# assert_generates("/items/list/1", { :controller => "items", :action => "list", :id => "1" })
+ #
+ # # Asserts that the generated route gives us our custom route
+ # assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" }
def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
clean_backtrace do
expected_path = "/#{expected_path}" unless expected_path[0] == ?/
@@ -67,9 +95,25 @@ module ActionController
end
end
- # Asserts that path and options match both ways; in other words, the URL generated from
- # options is the same as path, and also that the options recognized from path are the same as options. This
- # essentially combines assert_recognizes and assert_generates into one step.
+ # Asserts that path and options match both ways; in other words, it verifies that <tt>path</tt> generates
+ # <tt>options</tt> and then that <tt>options</tt> generates <tt>path</tt>. This essentially combines #assert_recognizes
+ # and #assert_generates into one step.
+ #
+ # The +extras+ hash allows you to specify options that would normally be provided as a query string to the action. The
+ # +message+ parameter allows you to specify a custom error message to display upon failure.
+ #
+ # ==== Examples
+ # # Assert a basic route: a controller with the default action (index)
+ # assert_routing('/home', :controller => 'home', :action => 'index')
+ #
+ # # Test a route generated with a specific controller, action, and parameter (id)
+ # assert_routing('/entries/show/23', :controller => 'entries', :action => 'show', id => 23)
+ #
+ # # Assert a basic route (controller + default action), with an error message if it fails
+ # assert_routing('/store', { :controller => 'store', :action => 'index' }, {}, {}, 'Route for store index not generated properly')
+ #
+ # # Tests a route, providing a defaults hash
+ # assert_routing 'controller/action/9', {:id => "9", :item => "square"}, {:controller => "controller", :action => "action"}, {}, {:item => "square"}
def assert_routing(path, options, defaults={}, extras={}, message=nil)
assert_recognizes(options, path, extras, message)
diff --git a/actionpack/lib/action_controller/assertions/selector_assertions.rb b/actionpack/lib/action_controller/assertions/selector_assertions.rb
index fd2f9dff9a..573405c0f9 100644
--- a/actionpack/lib/action_controller/assertions/selector_assertions.rb
+++ b/actionpack/lib/action_controller/assertions/selector_assertions.rb
@@ -13,15 +13,13 @@ module ActionController
end
# Adds the #assert_select method for use in Rails functional
- # test cases.
- #
- # Use #assert_select to make assertions on the response HTML of a controller
+ # test cases, which can be used to make assertions on the response HTML of a controller
# action. You can also call #assert_select within another #assert_select to
# make assertions on elements selected by the enclosing assertion.
#
# Use #css_select to select elements without making an assertions, either
# from the response HTML or elements selected by the enclosing assertion.
- #
+ #
# In addition to HTML responses, you can make the following assertions:
# * #assert_select_rjs -- Assertions on HTML content of RJS update and
# insertion operations.
@@ -29,7 +27,7 @@ module ActionController
# for example for dealing with feed item descriptions.
# * #assert_select_email -- Assertions on the HTML body of an e-mail.
#
- # Also see HTML::Selector for learning how to use selectors.
+ # Also see HTML::Selector to learn how to use selectors.
module SelectorAssertions
# :call-seq:
# css_select(selector) => array
@@ -49,12 +47,26 @@ module ActionController
# The selector may be a CSS selector expression (+String+), an expression
# with substitution values (+Array+) or an HTML::Selector object.
#
- # For example:
+ # ==== Examples
+ # # Selects all div tags
+ # divs = css_select("div")
+ #
+ # # Selects all paragraph tags and does something interesting
+ # pars = css_select("p")
+ # pars.each do |par|
+ # # Do something fun with paragraphs here...
+ # end
+ #
+ # # Selects all list items in unordered lists
+ # items = css_select("ul>li")
+ #
+ # # Selects all form tags and then all inputs inside the form
# forms = css_select("form")
# forms.each do |form|
# inputs = css_select(form, "input")
# ...
# end
+ #
def css_select(*args)
# See assert_select to understand what's going on here.
arg = args.shift
@@ -106,12 +118,13 @@ module ActionController
# response HTML. Calling #assert_select inside an #assert_select block will
# run the assertion for each element selected by the enclosing assertion.
#
- # For example:
+ # ==== Example
# assert_select "ol>li" do |elements|
# elements.each do |element|
# assert_select element, "li"
# end
# end
+ #
# Or for short:
# assert_select "ol>li" do
# assert_select "li"
@@ -149,7 +162,7 @@ module ActionController
# If the method is called with a block, once all equality tests are
# evaluated the block is called with an array of all matched elements.
#
- # === Examples
+ # ==== Examples
#
# # At least one form element
# assert_select "form"
@@ -353,7 +366,7 @@ module ActionController
# but without distinguishing whether the content is returned in an HTML
# or JavaScript.
#
- # === Examples
+ # ==== Examples
#
# # Replacing the element foo.
# # page.replace 'foo', ...
@@ -468,8 +481,20 @@ module ActionController
# The content of each element is un-encoded, and wrapped in the root
# element +encoded+. It then calls the block with all un-encoded elements.
#
- # === Example
+ # ==== Examples
+ # # Selects all bold tags from within the title of an ATOM feed's entries (perhaps to nab a section name prefix)
+ # assert_select_feed :atom, 1.0 do
+ # # Select each entry item and then the title item
+ # assert_select "entry>title" do
+ # # Run assertions on the encoded title elements
+ # assert_select_encoded do
+ # assert_select "b"
+ # end
+ # end
+ # end
+ #
#
+ # # Selects all paragraph tags from within the description of an RSS feed
# assert_select_feed :rss, 2.0 do
# # Select description element of each feed item.
# assert_select "channel>item>description" do
@@ -520,11 +545,19 @@ module ActionController
# You must enable deliveries for this assertion to work, use:
# ActionMailer::Base.perform_deliveries = true
#
- # === Example
+ # ==== Examples
+ #
+ # assert_select_email do
+ # assert_select "h1", "Email alert"
+ # end
+ #
+ # assert_select_email do
+ # items = assert_select "ol>li"
+ # items.each do
+ # # Work with items here...
+ # end
+ # end
#
- # assert_select_email do
- # assert_select "h1", "Email alert"
- # end
def assert_select_email(&block)
deliveries = ActionMailer::Base.deliveries
assert !deliveries.empty?, "No e-mail in delivery list"
diff --git a/actionpack/lib/action_controller/assertions/tag_assertions.rb b/actionpack/lib/action_controller/assertions/tag_assertions.rb
index f8d2ec9798..4ac489520a 100644
--- a/actionpack/lib/action_controller/assertions/tag_assertions.rb
+++ b/actionpack/lib/action_controller/assertions/tag_assertions.rb
@@ -3,6 +3,7 @@ require 'html/document'
module ActionController
module Assertions
+ # Pair of assertions to testing elements in the HTML output of the response.
module TagAssertions
# Asserts that there is a tag/node/element in the body of the response
# that meets all of the given conditions. The +conditions+ parameter must
@@ -48,39 +49,39 @@ module ActionController
# * if the condition is +true+, the value must not be +nil+.
# * if the condition is +false+ or +nil+, the value must be +nil+.
#
- # Usage:
+ # === Examples
#
- # # assert that there is a "span" tag
+ # # Assert that there is a "span" tag
# assert_tag :tag => "span"
#
- # # assert that there is a "span" tag with id="x"
+ # # Assert that there is a "span" tag with id="x"
# assert_tag :tag => "span", :attributes => { :id => "x" }
#
- # # assert that there is a "span" tag using the short-hand
+ # # Assert that there is a "span" tag using the short-hand
# assert_tag :span
#
- # # assert that there is a "span" tag with id="x" using the short-hand
+ # # Assert that there is a "span" tag with id="x" using the short-hand
# assert_tag :span, :attributes => { :id => "x" }
#
- # # assert that there is a "span" inside of a "div"
+ # # Assert that there is a "span" inside of a "div"
# assert_tag :tag => "span", :parent => { :tag => "div" }
#
- # # assert that there is a "span" somewhere inside a table
+ # # Assert that there is a "span" somewhere inside a table
# assert_tag :tag => "span", :ancestor => { :tag => "table" }
#
- # # assert that there is a "span" with at least one "em" child
+ # # Assert that there is a "span" with at least one "em" child
# assert_tag :tag => "span", :child => { :tag => "em" }
#
- # # assert that there is a "span" containing a (possibly nested)
+ # # Assert that there is a "span" containing a (possibly nested)
# # "strong" tag.
# assert_tag :tag => "span", :descendant => { :tag => "strong" }
#
- # # assert that there is a "span" containing between 2 and 4 "em" tags
+ # # Assert that there is a "span" containing between 2 and 4 "em" tags
# # as immediate children
# assert_tag :tag => "span",
# :children => { :count => 2..4, :only => { :tag => "em" } }
#
- # # get funky: assert that there is a "div", with an "ul" ancestor
+ # # Get funky: assert that there is a "div", with an "ul" ancestor
# # and an "li" parent (with "class" = "enum"), and containing a
# # "span" descendant that contains text matching /hello world/
# assert_tag :tag => "div",
@@ -105,6 +106,18 @@ module ActionController
# Identical to #assert_tag, but asserts that a matching tag does _not_
# exist. (See #assert_tag for a full discussion of the syntax.)
+ #
+ # === Examples
+ # # Assert that there is not a "div" containing a "p"
+ # assert_no_tag :tag => "div", :descendant => { :tag => "p" }
+ #
+ # # Assert that an unordered list is empty
+ # assert_no_tag :tag => "ul", :descendant => { :tag => "li" }
+ #
+ # # Assert that there is not a "p" tag with between 1 to 3 "img" tags
+ # # as immediate children
+ # assert_no_tag :tag => "p",
+ # :children => { :count => 1..3, :only => { :tag => "img" } }
def assert_no_tag(*opts)
clean_backtrace do
opts = opts.size > 1 ? opts.last.merge({ :tag => opts.first.to_s }) : opts.first