Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/branches/1-2-stable/actionpack/lib/action_controller/assertions.rb

Revision 5020, 3.7 kB (checked in by david, 2 years ago)

Better bundling of new assertions and make integration tests work again

Line 
1 require 'test/unit'
2 require 'test/unit/assertions'
3
4 module ActionController #:nodoc:
5   # In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions
6   # can be used against. These collections are:
7   #
8   # * assigns: Instance variables assigned in the action that are available for the view.
9   # * session: Objects being saved in the session.
10   # * flash: The flash objects currently in the session.
11   # * cookies: Cookies being sent to the user on this request.
12   #
13   # These collections can be used just like any other hash:
14   #
15   #   assert_not_nil assigns(:person) # makes sure that a @person instance variable was set
16   #   assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave"
17   #   assert flash.empty? # makes sure that there's nothing in the flash
18   #
19   # For historic reasons, the assigns hash uses string-based keys. So assigns[:person] won't work, but assigns["person"] will. To
20   # appease our yearning for symbols, though, an alternative accessor has been deviced using a method call instead of index referencing.
21   # So assigns(:person) will work just like assigns["person"], but again, assigns[:person] will not work.
22   #
23   # On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url.
24   #
25   # For redirects within the same controller, you can even call follow_redirect and the redirect will be followed, triggering another
26   # action call which can then be asserted against.
27   #
28   # == Manipulating the request collections
29   #
30   # The collections described above link to the response, so you can test if what the actions were expected to do happened. But
31   # sometimes you also want to manipulate these collections in the incoming request. This is really only relevant for sessions
32   # and cookies, though. For sessions, you just do:
33   #
34   #   @request.session[:key] = "value"
35   #
36   # For cookies, you need to manually create the cookie, like this:
37   #
38   #   @request.cookies["key"] = CGI::Cookie.new("key", "value")
39   #
40   # == Testing named routes
41   #
42   # If you're using named routes, they can be easily tested using the original named routes methods straight in the test case.
43   # Example:
44   #
45   #  assert_redirected_to page_url(:title => 'foo')
46   module Assertions
47     def self.included(klass)
48       klass.class_eval do
49         include ActionController::Assertions::ResponseAssertions
50         include ActionController::Assertions::SelectorAssertions
51         include ActionController::Assertions::RoutingAssertions
52         include ActionController::Assertions::TagAssertions
53         include ActionController::Assertions::DomAssertions
54         include ActionController::Assertions::ModelAssertions
55         include ActionController::Assertions::DeprecatedAssertions
56       end
57     end
58
59     def clean_backtrace(&block)
60       yield
61     rescue Test::Unit::AssertionFailedError => e         
62       path = File.expand_path(__FILE__)
63       raise Test::Unit::AssertionFailedError, e.message, e.backtrace.reject { |line| File.expand_path(line) =~ /#{path}/ }
64     end
65   end
66 end
67
68 require File.dirname(__FILE__) + '/assertions/response_assertions'
69 require File.dirname(__FILE__) + '/assertions/selector_assertions'
70 require File.dirname(__FILE__) + '/assertions/tag_assertions'
71 require File.dirname(__FILE__) + '/assertions/dom_assertions'
72 require File.dirname(__FILE__) + '/assertions/routing_assertions'
73 require File.dirname(__FILE__) + '/assertions/model_assertions'
74 require File.dirname(__FILE__) + '/assertions/deprecated_assertions'
75
76 module Test #:nodoc:
77   module Unit #:nodoc:
78     class TestCase #:nodoc:
79       include ActionController::Assertions
80     end
81   end
82 end
Note: See TracBrowser for help on using the browser.