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 73 73 def teardown 74 74 ActionMailer::Base.deliveries.clear 75 75 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 76 82 77 78 83 # 79 84 # Test assert select. 80 85 # … … 82 87 def test_assert_select 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 89 94 … … 131 136 end 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 152 169 … … 183 200 assert_select "#3", false 184 201 end 185 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" 207 end 208 end 186 209 end 187 210 188 211 -
actionpack/lib/action_controller/assertions/selector_assertions.rb
old new 196 196 # Otherwise just operate on the response document. 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 202 202 # accepts selector itself. … … 209 209 selector = arg 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 = {} 215 215 case arg = args.shift … … 277 277 # found one but expecting two. 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 290 286 # nested assert_select, which can be nested several levels deep. … … 300 296 # Returns all matches elements. 301 297 matches 302 298 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 304 310 # :call-seq: 305 311 # assert_select_rjs(id?) { |elements| ... } 306 312 # assert_select_rjs(statement, id?) { |elements| ... }