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

Changeset 6860

Show
Ignore:
Timestamp:
05/26/07 17:25:19 (1 year ago)
Author:
marcel
Message:

Make assert_select's failure messages clearer about what failed. #7779 [dchelimsky]

Files:

Legend:

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

    r6856 r6860  
    11*SVN* 
     2 
     3* Make assert_select's failure messages clearer about what failed. #7779 [dchelimsky] 
    24 
    35* Introduce a default respond_to block for custom types.  #8174 [Josh Peek] 
  • trunk/actionpack/lib/action_controller/assertions/selector_assertions.rb

    r6181 r6860  
    197197          root = response_from_page_or_rjs 
    198198        end 
    199  
     199         
    200200        # First or second argument is the selector: string and we pass 
    201201        # all remaining arguments. Array and we pass the argument. Also 
     
    210210          else raise ArgumentError, "Expecting a selector as the first argument" 
    211211        end 
    212  
     212         
    213213        # Next argument is used for equality tests. 
    214214        equals = {} 
     
    278278        message ||= content_mismatch if matches.empty? 
    279279        # Test minimum/maximum occurrence. 
    280         if equals[:minimum] 
    281           assert matches.size >= equals[:minimum], message || 
    282              "Expected at least #{equals[:minimum]} elements, found #{matches.size}." 
    283         end 
    284         if equals[:maximum] 
    285           assert matches.size <= equals[:maximum], message || 
    286             "Expected at most #{equals[:maximum]} elements, found #{matches.size}." 
    287         end 
     280        min, max = equals[:minimum], equals[:maximum] 
     281        message = message || %(Expected #{count_description(min, max)} matching "#{selector.to_s}", found #{matches.size}.) 
     282        assert matches.size >= min, message if min 
     283        assert matches.size <= max, message if max 
    288284 
    289285        # If a block is given call that block. Set @selected to allow 
     
    301297        matches 
    302298      end 
    303  
     299       
     300      def count_description(min, max) #:nodoc: 
     301        pluralize = lambda {|word, quantity| word << (quantity == 1 ? '' : 's')} 
     302         
     303        if min && max && (max != min) 
     304          "between #{min} and #{max} elements" 
     305        elsif min && !(min == 1 && max == 1) 
     306          "at least #{min} #{pluralize['element', min]}" 
     307        elsif max 
     308          "at most #{max} #{pluralize['element', max]}" 
     309        end 
     310      end 
     311       
    304312      # :call-seq: 
    305313      #   assert_select_rjs(id?) { |elements| ... } 
  • trunk/actionpack/test/controller/assert_select_test.rb

    r6119 r6860  
    7474    ActionMailer::Base.deliveries.clear 
    7575  end 
    76  
     76   
     77  def assert_failure(message, &block) 
     78    e = assert_raises(AssertionFailedError, &block) 
     79    assert_match(message, e.message) if Regexp === message 
     80    assert_equal(message, e.message) if String === message 
     81  end 
    7782 
    7883  # 
     
    8388    render_html %Q{<div id="1"></div><div id="2"></div>} 
    8489    assert_select "div", 2 
    85     assert_raises(AssertionFailedError) { assert_select "div", 3 } 
    86     assert_raises(AssertionFailedError){ assert_select "p" } 
     90    assert_failure(/Expected at least 3 elements matching \"div\", found 2/) { assert_select "div", 3 } 
     91    assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_select "p" } 
    8792  end 
    8893 
     
    132137 
    133138 
    134   def test_equality_of_instance
     139  def test_count
    135140    render_html %Q{<div id="1">foo</div><div id="2">foo</div>} 
    136141    assert_nothing_raised               { assert_select "div", 2 } 
    137     assert_raises(AssertionFailedError) { assert_select "div", 3 } 
     142    assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do 
     143      assert_select "div", 3 
     144    end 
    138145    assert_nothing_raised               { assert_select "div", 1..2 } 
    139     assert_raises(AssertionFailedError) { assert_select "div", 3..4 } 
     146    assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do 
     147      assert_select "div", 3..4 
     148    end 
    140149    assert_nothing_raised               { assert_select "div", :count=>2 } 
    141     assert_raises(AssertionFailedError) { assert_select "div", :count=>3 } 
     150    assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do 
     151      assert_select "div", :count=>3 
     152    end 
    142153    assert_nothing_raised               { assert_select "div", :minimum=>1 } 
    143154    assert_nothing_raised               { assert_select "div", :minimum=>2 } 
    144     assert_raises(AssertionFailedError) { assert_select "div", :minimum=>3 } 
     155    assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do 
     156      assert_select "div", :minimum=>3 
     157    end 
    145158    assert_nothing_raised               { assert_select "div", :maximum=>2 } 
    146159    assert_nothing_raised               { assert_select "div", :maximum=>3 } 
    147     assert_raises(AssertionFailedError) { assert_select "div", :maximum=>1 } 
     160    assert_failure(/Expected at most 1 element matching \"div\", found 2/) do 
     161      assert_select "div", :maximum=>1 
     162    end 
    148163    assert_nothing_raised               { assert_select "div", :minimum=>1, :maximum=>2 } 
    149     assert_raises(AssertionFailedError) { assert_select "div", :minimum=>3, :maximum=>4 } 
     164    assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do 
     165      assert_select "div", :minimum=>3, :maximum=>4 
     166    end 
    150167  end 
    151168 
     
    182199        assert_select "#2" 
    183200        assert_select "#3", false 
     201      end 
     202    end 
     203     
     204    assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do 
     205      assert_select "div" do 
     206        assert_select "#4" 
    184207      end 
    185208    end