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

Ticket #7779: assert_select_messages.patch

File assert_select_messages.patch, 5.7 kB (added by dchelimsky, 2 years ago)
  • actionpack/test/controller/assert_select_test.rb

    old new  
    7373  def teardown 
    7474    ActionMailer::Base.deliveries.clear 
    7575  end 
     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 
    7682 
    77  
    7883  # 
    7984  # Test assert select. 
    8085  # 
     
    8287  def test_assert_select 
    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 
    8994 
     
    131136  end 
    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 
    152169 
     
    183200        assert_select "#3", false 
    184201      end 
    185202    end 
     203     
     204    assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do 
     205      assert_select "div" do 
     206        assert_select "#4" 
     207      end 
     208    end 
    186209  end 
    187210 
    188211 
  • actionpack/lib/action_controller/assertions/selector_assertions.rb

    old new  
    196196          # Otherwise just operate on the response document. 
    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 
    202202        # accepts selector itself. 
     
    209209            selector = arg 
    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 = {} 
    215215        case arg = args.shift 
     
    277277        # found one but expecting two. 
    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 
    290286        # nested assert_select, which can be nested several levels deep. 
     
    300296        # Returns all matches elements. 
    301297        matches 
    302298      end 
    303  
     299       
     300      def count_description(min, max) #:nodoc: 
     301        if min && max && (max != min) 
     302          "between #{min} and #{max} elements" 
     303        elsif min && !(min == 1 && max == 1) 
     304          "at least #{min} element#{min == 1 ? '' : 's'}" 
     305        elsif max 
     306          "at most #{max} element#{max == 1 ? '' : 's'}" 
     307        end 
     308      end 
     309       
    304310      # :call-seq: 
    305311      #   assert_select_rjs(id?) { |elements| ... } 
    306312      #   assert_select_rjs(statement, id?) { |elements| ... }