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

Changeset 1189

Show
Ignore:
Timestamp:
04/17/05 11:38:01 (3 years ago)
Author:
david
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r1184 r1189  
    11*SVN* 
     2 
     3* 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. 
    24 
    35* Fixed DateHelper to return values on the option tags such that they'll work properly in IE with form_remote_tag #1024 [rscottmace@gmail.com] 
  • trunk/actionpack/lib/action_controller/assertions.rb

    r1065 r1189  
    55module Test #:nodoc: 
    66  module Unit #:nodoc: 
    7     # Adds a wealth of assertions to do functional testing of Action Controllers. 
     7    # In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions 
     8    # can be used against. These collections are: 
     9    # 
     10    # * assigns: Instance variables assigned in the action that's available for the view. 
     11    # * session: Objects being saved in the session. 
     12    # * flash: The flash objects being currently in the session. 
     13    # * cookies: Cookies being sent to the user on this request. 
     14    #  
     15    # These collections can be used just like any other hash: 
     16    # 
     17    #   assert_not_nil assigns[:person] # makes sure that a @person instance variable was set 
     18    #   assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave" 
     19    #   assert flash.empty? # makes sure that there's nothing in the flash 
     20    # 
     21    # On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url. 
     22    # 
     23    # For redirects within the same controller, you can even call follow_redirect and the redirect will be follow triggering another 
     24    # action call which can then be asserted against. 
    825    module Assertions 
    9       # -- basic assertions --------------------------------------------------- 
    10        
    11       # ensure that the web request has been serviced correctly 
    12       def assert_success(message=nil) 
    13         response = acquire_assertion_target 
    14         if response.success? 
    15           # to count the assertion 
    16           assert_block("") { true } 
     26      # Asserts that the response is one of the following types: 
     27      #  
     28      # * <tt>:success</tt>: Status code was 200 
     29      # * <tt>:redirect</tt>: Status code was in the 300-399 range 
     30      # * <tt>:missing</tt>: Status code was 404 
     31      # * <tt>:error</tt>:  Status code was in the 500-599 range 
     32      # 
     33      # You can also pass an explicit status code number as the type, like assert_response(501) 
     34      def assert_response(type, message = nil) 
     35        if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?") 
     36          assert_block("") { true } # to count the assertion 
     37        elsif type.is_a?(Fixnum) && @response.response_code == type 
     38          assert_block("") { true } # to count the assertion 
    1739        else 
    18           if response.redirect? 
    19             msg = build_message(message, "Response unexpectedly redirect to <?>", response.redirect_url) 
    20           else 
    21             msg = build_message(message, "unsuccessful request (response code = <?>)",  
    22                 response.response_code) 
    23           end 
    24           assert_block(msg) { false } 
     40          assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false } 
    2541        end 
    2642      end 
    2743 
    28       # ensure the request was rendered with the appropriate template file 
    29       def assert_rendered_file(expected=nil, message=nil) 
    30         response = acquire_assertion_target 
    31         rendered = expected ? response.rendered_file(!expected.include?('/')) : response.rendered_file 
     44      # Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, 
     45      # such at assert_redirected_to(:controller => "weblog") will also match the redirection of  
     46      # redirect_to(:controller => "weblog", :action => "show") and so on. 
     47      def assert_redirected_to(options = {}, message=nil) 
     48        assert_redirect(message) 
     49 
     50        msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>)", @response.redirected_to) 
     51        assert_block(msg) do 
     52          if options.is_a?(Symbol) 
     53            @response.redirected_to == options 
     54          else 
     55            options.keys.all? do |k|  
     56              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]) 
     57            end 
     58          end 
     59        end 
     60      end 
     61 
     62      # Asserts that the request was rendered with the appropriate template file 
     63      def assert_template(expected=nil, message=nil) 
     64        rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file 
    3265        msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered) 
    3366        assert_block(msg) do 
    3467          if expected.nil? 
    35             response.rendered_with_file? 
     68            @response.rendered_with_file? 
    3669          else 
    3770            expected == rendered 
     
    3972        end 
    4073      end 
     74 
     75      alias_method :assert_rendered_file, :assert_template #:nodoc: 
    4176       
    42       # -- session assertions ------------------------------------------------- 
    43  
    44       # ensure that the session has an object with the specified name 
    45       def assert_session_has(key=nil, message=nil) 
    46         response = acquire_assertion_target 
    47         msg = build_message(message, "<?> is not in the session <?>", key, response.session) 
    48         assert_block(msg) { response.has_session_object?(key) } 
    49       end 
    50  
    51       # ensure that the session has no object with the specified name 
    52       def assert_session_has_no(key=nil, message=nil) 
    53         response = acquire_assertion_target 
    54         msg = build_message(message, "<?> is in the session <?>", key, response.session) 
    55         assert_block(msg) { !response.has_session_object?(key) } 
    56       end 
    57        
    58       def assert_session_equal(expected = nil, key = nil, message = nil) 
    59         response = acquire_assertion_target 
    60         msg = build_message(message, "<?> expected in session['?'] but was <?>", expected, key, response.session[key]) 
    61         assert_block(msg) { expected == response.session[key] } 
    62       end 
    63  
    64       # -- cookie assertions --------------------------------------------------- 
    65  
    66       def assert_no_cookie(key = nil, message = nil) 
    67         response = acquire_assertion_target 
    68         actual = response.cookies[key] 
    69         msg = build_message(message, "<?> not expected in cookies['?']", actual, key) 
    70         assert_block(msg) { actual.nil? or actual.empty? } 
    71       end 
    72        
    73       def assert_cookie_equal(expected = nil, key = nil, message = nil) 
    74         response = acquire_assertion_target 
    75         actual = response.cookies[key] 
    76         actual = actual.first if actual 
    77         msg = build_message(message, "<?> expected in cookies['?'] but was <?>", expected, key, actual) 
    78         assert_block(msg) { expected == actual } 
    79       end 
    80        
    81       # -- flash assertions --------------------------------------------------- 
    82  
    83       # ensure that the flash has an object with the specified name 
    84       def assert_flash_has(key=nil, message=nil) 
    85         response = acquire_assertion_target 
    86         msg = build_message(message, "<?> is not in the flash <?>", key, response.flash) 
    87         assert_block(msg) { response.has_flash_object?(key) } 
    88       end 
    89  
    90       # ensure that the flash has no object with the specified name 
    91       def assert_flash_has_no(key=nil, message=nil) 
    92         response = acquire_assertion_target 
    93         msg = build_message(message, "<?> is in the flash <?>", key, response.flash) 
    94         assert_block(msg) { !response.has_flash_object?(key) } 
    95       end 
    96  
    97       # ensure the flash exists 
    98       def assert_flash_exists(message=nil) 
    99         response = acquire_assertion_target 
    100         msg = build_message(message, "the flash does not exist <?>", response.session['flash'] ) 
    101         assert_block(msg) { response.has_flash? } 
    102       end 
    103  
    104       # ensure the flash does not exist 
    105       def assert_flash_not_exists(message=nil) 
    106         response = acquire_assertion_target 
    107         msg = build_message(message, "the flash exists <?>", response.flash) 
    108         assert_block(msg) { !response.has_flash? } 
    109       end 
    110        
    111       # ensure the flash is empty but existent 
    112       def assert_flash_empty(message=nil) 
    113         response = acquire_assertion_target 
    114         msg = build_message(message, "the flash is not empty <?>", response.flash) 
    115         assert_block(msg) { !response.has_flash_with_contents? } 
    116       end 
    117  
    118       # ensure the flash is not empty 
    119       def assert_flash_not_empty(message=nil) 
    120         response = acquire_assertion_target 
    121         msg = build_message(message, "the flash is empty") 
    122         assert_block(msg) { response.has_flash_with_contents? } 
    123       end 
    124        
    125       def assert_flash_equal(expected = nil, key = nil, message = nil) 
    126         response = acquire_assertion_target 
    127         msg = build_message(message, "<?> expected in flash['?'] but was <?>", expected, key, response.flash[key]) 
    128         assert_block(msg) { expected == response.flash[key] } 
    129       end 
    130        
    131       # -- redirection assertions --------------------------------------------- 
    132  
    133       # ensure we have be redirected 
    134       def assert_redirect(message=nil) 
    135         response = acquire_assertion_target 
    136         msg = build_message(message, "response is not a redirection (response code is <?>)", response.response_code) 
    137         assert_block(msg) { response.redirect? } 
    138       end 
    139  
    140       def assert_redirected_to(options = {}, message=nil) 
    141         assert_redirect(message) 
    142         response = acquire_assertion_target 
    143  
    144         msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>)", response.redirected_to) 
    145         assert_block(msg) do 
    146           if options.is_a?(Symbol) 
    147             response.redirected_to == options 
    148           else 
    149             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] ) } 
    150           end 
    151         end 
    152       end 
    153        
    154       # ensure our redirection url is an exact match 
    155       def assert_redirect_url(url=nil, message=nil) 
    156         assert_redirect(message) 
    157         response = acquire_assertion_target 
    158         msg = build_message(message, "<?> is not the redirected location <?>", url, response.redirect_url) 
    159         assert_block(msg) { response.redirect_url == url } 
    160       end 
    161  
    162       # ensure our redirection url matches a pattern 
    163       def assert_redirect_url_match(pattern=nil, message=nil) 
    164         assert_redirect(message) 
    165         response = acquire_assertion_target 
    166         msg = build_message(message, "<?> was not found in the location: <?>", pattern, response.redirect_url) 
    167         assert_block(msg) { response.redirect_url_match?(pattern) } 
    168       end 
    169  
    17077      # -- routing assertions -------------------------------------------------- 
    17178 
     
    17481        # Load routes.rb if it hasn't been loaded. 
    17582        ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?  
    176          
     83       
    17784        # Assume given controller 
    17885        request = ActionController::TestRequest.new({}, {}, nil) 
    17986        request.path = path 
    18087        ActionController::Routing::Routes.recognize!(request) 
    181          
     88       
    18289        expected_options = expected_options.clone 
    18390        extras.each_key { |key| expected_options.delete key } unless extras.nil? 
    184          
     91       
    18592        msg = build_message(message, "The recognized options <?> did not match <?>",  
    18693            request.path_parameters, expected_options) 
     
    19299        # Load routes.rb if it hasn't been loaded. 
    193100        ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?  
    194          
     101       
    195102        # Assume given controller 
    196103        request = ActionController::TestRequest.new({}, {}, nil) 
    197104        request.path_parameters = (defaults or {}).clone 
    198105        request.path_parameters[:controller] ||= options[:controller] 
    199          
     106       
    200107        generated_path, found_extras = ActionController::Routing::Routes.generate(options, request) 
    201108        generated_path = generated_path.join('/') 
    202109        msg = build_message(message, "found extras <?>, not <?>", found_extras, extras) 
    203110        assert_block(msg) { found_extras == extras } 
    204          
     111       
    205112        msg = build_message(message, "The generated path <?> did not match <?>", generated_path,  
    206113            expected_path) 
     
    214121        assert_generates(path, options, defaults, extras, message) 
    215122      end 
    216        
    217       # -- template assertions ------------------------------------------------ 
    218  
    219       # ensure that a template object with the given name exists 
    220       def assert_template_has(key=nil, message=nil) 
    221         response = acquire_assertion_target 
    222         msg = build_message(message, "<?> is not a template object", key ) 
    223         assert_block(msg) { response.has_template_object?(key) } 
    224       end 
    225  
    226       # ensure that a template object with the given name does not exist 
    227       def assert_template_has_no(key=nil,message=nil) 
    228         response = acquire_assertion_target 
    229         msg = build_message(message, "<?> is a template object <?>", key, response.template_objects[key]) 
    230         assert_block(msg) { !response.has_template_object?(key) } 
    231       end 
    232  
    233       # ensures that the object assigned to the template on +key+ is equal to +expected+ object. 
    234       def assert_assigned_equal(expected = nil, key = nil, message = nil) 
    235         response = acquire_assertion_target 
    236         msg = build_message(message, "<?> expected in assigns['?'] but was <?>", expected, key, response.template.assigns[key.to_s]) 
    237         assert_block(msg) { expected == response.template.assigns[key.to_s] } 
    238       end 
    239  
    240       # Asserts that the template returns the +expected+ string or array based on the XPath +expression+. 
    241       # This will only work if the template rendered a valid XML document. 
    242       def assert_template_xpath_match(expression=nil, expected=nil, message=nil) 
    243         response = acquire_assertion_target 
    244         xml, matches = REXML::Document.new(response.body), [] 
    245         xml.elements.each(expression) { |e| matches << e.text } 
    246         if matches.empty? then 
    247           msg = build_message(message, "<?> not found in document", expression) 
    248           flunk(msg) 
    249           return 
    250         elsif matches.length < 2 then 
    251           matches = matches.first 
    252         end 
    253  
    254         msg = build_message(message, "<?> found <?>, not <?>", expression, matches, expected) 
    255         assert_block(msg) { matches == expected } 
    256       end 
    257        
    258       # -- helper functions --------------------------------------------------- 
    259         
    260       # get the TestResponse object that these assertions depend upon  
    261       def acquire_assertion_target 
    262         target = ActionController::TestResponse.assertion_target 
    263         assert_block( "Unable to acquire the TestResponse.assertion_target.  Please set this before calling this assertion." ) { !target.nil? } 
    264         target 
    265       end 
    266        
    267     end # Assertions 
    268   end # Unit 
    269 end # Test 
     123    end 
     124  end 
     125end 
  • trunk/actionpack/lib/action_controller/flash.rb

    r942 r1189  
    2525  # See docs on the FlashHash class for more details about the flash. 
    2626  module Flash 
    27  
    2827    def self.append_features(base) #:nodoc: 
    2928      super 
     
    4544     
    4645    class FlashHash < Hash 
    47      
    4846      def initialize #:nodoc: 
    4947        super 
     
    114112     
    115113      private 
    116      
    117       # Used internally by the <tt>keep</tt> and <tt>discard</tt> methods 
    118       #     use()               # marks the entire flash as used 
    119       #     use('msg')          # marks the "msg" entry as used 
    120       #     use(nil, false)     # marks the entire flash as unused (keeps it around for one more action) 
    121       #     use('msg', false)   # marks the "msg" entry as unused (keeps it around for one more action
    122       def use(k=nil, v=true) 
    123         unless k.nil? 
    124           @used[k] = v 
    125         else 
    126           keys.each{|key| use key, v } 
     114        # Used internally by the <tt>keep</tt> and <tt>discard</tt> methods 
     115        #     use()               # marks the entire flash as used 
     116        #     use('msg')          # marks the "msg" entry as used 
     117        #     use(nil, false)     # marks the entire flash as unused (keeps it around for one more action) 
     118        #     use('msg', false)   # marks the "msg" entry as unused (keeps it around for one more action) 
     119        def use(k=nil, v=true
     120          unless k.nil? 
     121            @used[k] = v 
     122          else 
     123            keys.each{|key| use key, v } 
     124          end 
    127125        end 
    128       end 
    129  
    130126    end 
    131127      
    132128    
    133129    protected  
     130      # Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or  
     131      # <tt>flash["notice"] = "hello"</tt> to put a new one. 
     132      def flash #:doc: 
     133        @session['flash'] ||= FlashHash.new 
     134      end 
    134135   
    135     # Access the contents of the flash. Use <tt>flash["notice"]</tt> to read a notice you put there or  
    136     # <tt>flash["notice"] = "hello"</tt> to put a new one. 
    137     def flash #:doc: 
    138       @session['flash'] ||= FlashHash.new 
    139     end 
    140    
    141     # deprecated. use <tt>flash.keep</tt> instead 
    142     def keep_flash #:doc: 
    143       flash.keep 
    144     end 
     136      # deprecated. use <tt>flash.keep</tt> instead 
     137      def keep_flash #:doc: 
     138        flash.keep 
     139      end 
    145140   
    146141   
    147     private 
     142      private 
    148143   
    149     # marks flash entries as used and expose the flash to the view  
    150     def fire_flash 
    151       flash.discard 
    152       @assigns["flash"] = flash 
    153     end 
     144      # marks flash entries as used and expose the flash to the view  
     145      def fire_flash 
     146        flash.discard 
     147        @assigns["flash"] = flash 
     148      end 
    154149   
    155     # deletes the flash entries that were not marked for keeping 
    156     def sweep_flash 
    157       flash.sweep 
    158     end 
    159    
     150      # deletes the flash entries that were not marked for keeping 
     151      def sweep_flash 
     152        flash.sweep 
     153      end   
    160154  end 
    161155end 
  • trunk/actionpack/lib/action_controller/test_process.rb

    r1175 r1189  
    1 require File.dirname(__FILE__) + '/assertions/action_pack_assertions
    2 require File.dirname(__FILE__) + '/assertions/active_record_assertions' 
     1require File.dirname(__FILE__) + '/assertions
     2require File.dirname(__FILE__) + '/deprecated_assertions' 
    33 
    44if defined?(RAILS_ROOT) 
     
    9595   
    9696  class TestResponse < AbstractResponse #:nodoc: 
    97     # the class attribute ties a TestResponse to the assertions  
    98     class << self 
    99       attr_accessor :assertion_target 
    100     end 
    101  
    102     # initializer 
    103     def initialize 
    104       TestResponse.assertion_target=self# if TestResponse.assertion_target.nil? 
    105       super() 
    106     end 
    107      
    10897    # the response code of the request 
    10998    def response_code 
     
    127116     
    128117    # was there a server-side error? 
    129     def server_error? 
     118    def error? 
    130119      (500..599).include?(response_code) 
    131120    end 
     121 
     122    alias_method :server_error?, :error? 
    132123 
    133124    # returns the redirection location or nil 
     
    282273        end 
    283274 
    284         def assigns(name) 
    285           @response.template.assigns[name.to_s] 
     275        def assigns(key = nil) 
     276          if key.nil? 
     277            @response.template.assigns 
     278          else 
     279            @response.template.assigns[key.to_s] 
     280          end 
     281        end 
     282         
     283        def session 
     284          @response.session 
     285        end 
     286 
     287        def flash 
     288          @response.flash 
     289        end 
     290 
     291        def cookies 
     292          @response.cookies 
     293        end 
     294 
     295        def redirect_to_url 
     296          @response.redirect_url 
    286297        end 
    287298 
  • trunk/actionpack/lib/action_view/base.rb

    r1169 r1189  
    120120    include ERB::Util 
    121121     
    122     attr_reader :first_render 
     122    attr_reader   :first_render 
    123123    attr_accessor :base_path, :assigns, :template_extension 
    124124    attr_accessor :controller 
     
    158158 
    159159    def initialize(base_path = nil, assigns_for_first_render = {}, controller = nil)#:nodoc: 
    160       @base_path, @assigns = base_path, assigns_for_first_render 
     160      @base_path, @assigns = base_path, assigns_for_first_render.with_indifferent_access 
    161161      @controller = controller 
    162162    end 
  • trunk/actionpack/test/controller/action_pack_assertions_test.rb

    r942 r1189  
    171171    assert @response.has_flash_with_contents? 
    172172    assert_flash_exists 
    173     assert ActionController::TestResponse.assertion_target.has_flash_with_contents? 
    174173    assert_flash_not_empty 
    175174    assert_flash_has 'hello'