public
Description: have_tag() without Rails' assert_select()
Clone URL: git://github.com/pd/rspec_hpricot_matchers.git
include count details in failure messages

:matches? has grown big and ugly, but i want some assurance that
all of these messages are legible and make sense before i start
refactoring too heavily
pd (author)
Wed Mar 05 15:47:15 -0800 2008
commit  7290907535f0c4fdbbb0bde0e1df6270a9e675b7
tree    29b410eec38f318b1e5bf5296db7b6d375ea17f7
parent  14c7c6916f32718f046f8a492b545e440e5f3b90
...
47
48
49
 
50
51
52
...
61
62
63
64
65
 
 
 
 
66
67
68
69
70
 
 
 
71
72
73
...
47
48
49
50
51
52
53
...
62
63
64
 
 
65
66
67
68
69
70
71
72
 
73
74
75
76
77
78
0
@@ -47,6 +47,7 @@ module RspecHpricotMatchers
0
         end
0
       end
0
 
0
+ @actual_count = matched_elements.length
0
       if @options[:count]
0
         return false unless @options[:count] === matched_elements.length
0
       end
0
@@ -61,13 +62,17 @@ module RspecHpricotMatchers
0
     end
0
 
0
     def failure_message
0
- with_inner_text = @inner_text ? " with inner text #{@inner_text.inspect}" : ""
0
- "expected #{@actual.inspect} to have tag #{@selector.inspect}#{with_inner_text}, but did not"
0
+ with_inner_text = @inner_text ? " with inner text #{@inner_text.inspect}" : ""
0
+ explanation = @actual_count ? "but found #{@actual_count}" : "but did not"
0
+ count_explanation = @options[:count] ? "#{@options[:count]} elements matching" : "an element matching"
0
+ "expected\n#{@hdoc.to_s}\nto have #{count_explanation} #{@selector.inspect}#{with_inner_text}, #{explanation}"
0
     end
0
 
0
     def negative_failure_message
0
       with_inner_text = @inner_text ? " with inner text #{@inner_text.inspect}" : ""
0
- "did not expect #{@actual.inspect} to have tag #{@selector.inspect}#{with_inner_text}, but did"
0
+ explanation = @actual_count ? "but found #{@actual_count}" : "but did"
0
+ count_explanation = @options[:count] ? "#{@options[:count]} elements matching" : "an element matching"
0
+ "did not expect\n#{@hdoc.to_s}\nto have #{count_explanation} #{@selector.inspect}#{with_inner_text}, #{explanation}"
0
     end
0
   end
0
 
...
168
169
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
0
@@ -168,4 +168,31 @@ describe 'have_tag with counts' do
0
       ul.should have_tag('li', /foo/i, :count => 2)
0
     end
0
   end
0
+
0
+ it "should include the actual number of elements matched in the failure message" do
0
+ lambda {
0
+ @html.should have_tag('li', :count => 3)
0
+ }.should raise_error(SpecFailed, /found 4/)
0
+ lambda {
0
+ @html.should have_tag('li', :count => 5)
0
+ }.should raise_error(SpecFailed, /found 4/)
0
+ end
0
+
0
+ it "should include the actual number of elements matched in the negative failure message" do
0
+ lambda {
0
+ @html.should_not have_tag('li', :count => 4)
0
+ }.should raise_error(SpecFailed, /found 4/)
0
+ end
0
+
0
+ it "should include the expected number of elements in the failure message" do
0
+ lambda {
0
+ @html.should have_tag('li', :count => 2)
0
+ }.should raise_error(SpecFailed, /to have 2 elements matching/)
0
+ end
0
+
0
+ it "should include the expected number of elements in the negative failure message" do
0
+ lambda {
0
+ @html.should_not have_tag('li', :count => 4)
0
+ }.should raise_error(SpecFailed, /to have 4 elements matching/)
0
+ end
0
 end

Comments

    No one has commented yet.