root/trunk/actionpack/lib/action_controller/assertions.rb
| Revision 8301, 3.0 kB (checked in by david, 1 year ago) |
|---|
| Line | |
|---|---|
| 1 | require 'test/unit/assertions' |
| 2 | |
| 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: |
| 6 | # |
| 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. |
| 11 | # |
| 12 | # These collections can be used just like any other hash: |
| 13 | # |
| 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 |
| 17 | # |
| 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. |
| 21 | # |
| 22 | # On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url. |
| 23 | # |
| 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. |
| 26 | # |
| 27 | # == Manipulating the request collections |
| 28 | # |
| 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: |
| 32 | # |
| 33 | # @request.session[:key] = "value" |
| 34 | # |
| 35 | # For cookies, you need to manually create the cookie, like this: |
| 36 | # |
| 37 | # @request.cookies["key"] = CGI::Cookie.new("key", "value") |
| 38 | # |
| 39 | # == Testing named routes |
| 40 | # |
| 41 | # If you're using named routes, they can be easily tested using the original named routes' methods straight in the test case. |
| 42 | # Example: |
| 43 | # |
| 44 | # assert_redirected_to page_url(:title => 'foo') |
| 45 | module Assertions |
| 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") } |
| 50 | end |
| 51 | end |
| 52 | |
| 53 | def clean_backtrace(&block) |
| 54 | yield |
| 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 } |
| 58 | raise |
| 59 | end |
| 60 | end |
| 61 | end |
| 62 | |
| 63 | module Test #:nodoc: |
| 64 | module Unit #:nodoc: |
| 65 | class TestCase #:nodoc: |
| 66 | include ActionController::Assertions |
| 67 | end |
| 68 | end |
| 69 | end |
Note: See TracBrowser for help on using the browser.