Changeset 6860
- Timestamp:
- 05/26/07 17:25:19 (1 year ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/assertions/selector_assertions.rb (modified) (4 diffs)
- trunk/actionpack/test/controller/assert_select_test.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r6856 r6860 1 1 *SVN* 2 3 * Make assert_select's failure messages clearer about what failed. #7779 [dchelimsky] 2 4 3 5 * Introduce a default respond_to block for custom types. #8174 [Josh Peek] trunk/actionpack/lib/action_controller/assertions/selector_assertions.rb
r6181 r6860 197 197 root = response_from_page_or_rjs 198 198 end 199 199 200 200 # First or second argument is the selector: string and we pass 201 201 # all remaining arguments. Array and we pass the argument. Also … … 210 210 else raise ArgumentError, "Expecting a selector as the first argument" 211 211 end 212 212 213 213 # Next argument is used for equality tests. 214 214 equals = {} … … 278 278 message ||= content_mismatch if matches.empty? 279 279 # 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 288 284 289 285 # If a block is given call that block. Set @selected to allow … … 301 297 matches 302 298 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 304 312 # :call-seq: 305 313 # assert_select_rjs(id?) { |elements| ... } trunk/actionpack/test/controller/assert_select_test.rb
r6119 r6860 74 74 ActionMailer::Base.deliveries.clear 75 75 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 77 82 78 83 # … … 83 88 render_html %Q{<div id="1"></div><div id="2"></div>} 84 89 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" } 87 92 end 88 93 … … 132 137 133 138 134 def test_ equality_of_instances139 def test_counts 135 140 render_html %Q{<div id="1">foo</div><div id="2">foo</div>} 136 141 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 138 145 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 140 149 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 142 153 assert_nothing_raised { assert_select "div", :minimum=>1 } 143 154 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 145 158 assert_nothing_raised { assert_select "div", :maximum=>2 } 146 159 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 148 163 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 150 167 end 151 168 … … 182 199 assert_select "#2" 183 200 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" 184 207 end 185 208 end