1 require 'test/unit/assertions'
3 module ActionController
#:nodoc:
4 # In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions
5 # can be used against. These collections are:
7 # * assigns: Instance variables assigned in the action that are available for the view.
8 # * session: Objects being saved in the session.
9 # * flash: The flash objects currently in the session.
10 # * cookies: Cookies being sent to the user on this request.
12 # These collections can be used just like any other hash:
14 # assert_not_nil assigns(:person) # makes sure that a @person instance variable was set
15 # assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave"
16 # assert flash.empty? # makes sure that there's nothing in the flash
18 # For historic reasons, the assigns hash uses string-based keys. So assigns[:person] won't work, but assigns["person"] will. To
19 # appease our yearning for symbols, though, an alternative accessor has been devised using a method call instead of index referencing.
20 # So assigns(:person) will work just like assigns["person"], but again, assigns[:person] will not work.
22 # On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url.
24 # For redirects within the same controller, you can even call follow_redirect and the redirect will be followed, triggering another
25 # action call which can then be asserted against.
27 # == Manipulating the request collections
29 # The collections described above link to the response, so you can test if what the actions were expected to do happened. But
30 # sometimes you also want to manipulate these collections in the incoming request. This is really only relevant for sessions
31 # and cookies, though. For sessions, you just do:
33 # @request.session[:key] = "value"
35 # For cookies, you need to manually create the cookie, like this:
37 # @request.cookies["key"] = CGI::Cookie.new("key", "value")
39 # == Testing named routes
41 # If you're using named routes, they can be easily tested using the original named routes' methods straight in the test case.
44 # assert_redirected_to page_url(:title => 'foo')
46 def self.included(klass
)
47 %w(response selector tag dom routing model
).each
do |kind
|
48 require "action_controller/assertions/#{kind}_assertions"
49 klass
.module_eval
{ include const_get("#{kind.camelize}Assertions") }
53 def clean_backtrace(&block
)
55 rescue Test
::Unit::AssertionFailedError => error
56 framework_path
= Regexp
.new(File
.expand_path("#{File.dirname(__FILE__)}/assertions"))
57 error
.backtrace
.reject
! { |line
| File
.expand_path(line
) =~ framework_path
}
65 class TestCase
#:nodoc:
66 include ActionController
::Assertions