/
matchers_spec.rb
322 lines (254 loc) · 10.6 KB
/
matchers_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe Webrat::Matchers do
include Webrat::Matchers
include Webrat::HaveTagMatcher
before(:each) do
@body = <<-HTML
<div id='main'>
<div class='inner'>hello, world!</div>
<ul>
<li>First</li>
<li>Second</li>
</ul>
</div>
HTML
end
describe "#have_xpath" do
it "should work with non-HTML documents" do
xml = '<foo bar="baz"></foo>'
xml.should have_xpath('/foo[@bar="baz"]')
end
it "should be able to match an XPATH" do
@body.should have_xpath("//div")
end
it "should not match a XPATH that does not exist" do
@body.should_not have_xpath("//p")
end
it "should be able to loop over all the matched elements" do
@body.should have_xpath("//div") { |node| node.first.name.should == "div" }
end
it "should not match of any of the matchers in the block fail" do
lambda {
@body.should have_xpath("//div") { |node| node.first.name.should == "p" }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
it "should be able to use #have_xpath in the block" do
@body.should have_xpath("//div[@id='main']") { |node| node.should have_xpath("./div[@class='inner']") }
end
it "should convert absolute paths to relative in the block" do
@body.should have_xpath("//div[@id='main']") { |node| node.should have_xpath("//div[@class='inner']") }
end
it "should not match any parent tags in the block" do
lambda {
@body.should have_xpath("//div[@class='inner']") { |node| node.should have_xpath("//div[@id='main']") }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
describe 'asserts for xpath' do
before(:each) do
should_receive(:response_body).and_return @body
require 'test/unit'
end
describe "assert_have_xpath" do
it "should pass when body contains the selection" do
assert_have_xpath("//div")
end
it "should throw an exception when the body doesnt have matching xpath" do
lambda {assert_have_xpath("//p")}.should raise_error(Test::Unit::AssertionFailedError)
end
end
describe "assert_have_no_xpath" do
it "should pass when the body doesn't contan the xpath" do
assert_have_no_xpath("//p")
end
it "should throw an exception when the body does contain the xpath" do
lambda {assert_have_no_xpath("//div")}.should raise_error(Test::Unit::AssertionFailedError)
end
end
end
end
describe "#have_selector" do
it "should be able to match a CSS selector" do
@body.should have_selector("div")
end
it "should not match a CSS selector that does not exist" do
@body.should_not have_selector("p")
end
it "should be able to loop over all the matched elements" do
@body.should have_selector("div") { |node| node.first.name.should == "div" }
end
it "should not match of any of the matchers in the block fail" do
lambda {
@body.should have_selector("div") { |node| node.first.name.should == "p" }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
it "should be able to use #have_selector in the block" do
@body.should have_selector("#main") { |node| node.should have_selector(".inner") }
end
it "should not match any parent tags in the block" do
lambda {
@body.should have_selector(".inner") { |node| node.should have_selector("#main") }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
describe "asserts for selector," do
before(:each) do
should_receive(:response_body).and_return @body
require 'test/unit'
end
describe "assert_have_selector" do
it "should pass when body contains the selection" do
assert_have_selector("div")
end
it "should throw an exception when the body doesnt have matching selection" do
lambda {assert_have_selector("p")}.should raise_error(Test::Unit::AssertionFailedError)
end
end
describe "assert_have_not_selector" do
it "should pass when the body doesn't contan the selection" do
assert_have_no_selector("p")
end
it "should throw an exception when the body does contain the selection" do
lambda {assert_have_no_selector("div")}.should raise_error(Test::Unit::AssertionFailedError)
end
end
end
end
describe "#have_tag" do
it "should be able to match a tag" do
@body.should have_tag("div")
end
it "should not match the tag when it should not match" do
@body.should_not have_tag("p")
end
it "should be able to specify the content of the tag" do
@body.should have_tag("div", :content => "hello, world!")
end
it "should be able to specify the attributes of the tag" do
@body.should have_tag("div", :class => "inner")
end
it "should be able to loop over all the matched elements" do
@body.should have_tag("div") { |node| node.first.name.should == "div" }
end
it "should not match of any of the matchers in the block fail" do
lambda {
@body.should have_tag("div") { |node| node.first.name.should == "p" }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
it "should be able to use #have_tag in the block" do
@body.should have_tag("div", :id => "main") { |node| node.should have_tag("div", :class => "inner") }
end
it "should not match any parent tags in the block" do
lambda {
@body.should have_tag("div", :class => "inner") { |node| node.should have_tag("div", :id => "main") }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
it "should work with items that have multiple child nodes" do
@body.should have_tag("ul") { |n|
n.should have_tag("li", :content => "First")
n.should have_tag("li", :content => "Second")
}
end
describe "asserts for tags," do
before(:each) do
should_receive(:response_body).and_return @body
require 'test/unit'
end
describe "assert_tag" do
it "should pass when body contains the tag" do
assert_tag("div")
end
it "should pass when finding with additional selectors" do
assert_tag("div", :class => "inner")
end
it "should throw an exception when the body doesnt have matching tag" do
lambda {assert_tag("p")}.should raise_error(Test::Unit::AssertionFailedError)
end
it "should throw an exception when the body doens't have a tag matching the additional selector" do
lambda {assert_tag("div", :class => "nope")}.should raise_error(Test::Unit::AssertionFailedError)
end
end
describe "assert_no_tag" do
it "should pass when the body doesn't contan the tag" do
assert_no_tag("p")
end
it "should pass when the body doesn't contain the tag due to additional selectors missing" do
assert_no_tag("div", :class => "nope")
end
it "should throw an exception when the body does contain the tag" do
lambda {assert_no_tag("div")}.should raise_error(Test::Unit::AssertionFailedError)
end
it "should throw an exception when the body contains the tag with additional selectors" do
lambda {assert_no_tag("div", :class => "inner")}.should raise_error(Test::Unit::AssertionFailedError)
end
end
end
end
describe Webrat::Matchers::HasContent do
include Webrat::Matchers
before(:each) do
@body = <<-EOF
<div id='main'>
<div class='inner'>hello, world!</div>
</div>
EOF
end
describe "#matches?" do
it "should call element#contains? when the argument is a string" do
@body.should contain("hello, world!")
end
it "should call element#matches? when the argument is a regular expression" do
@body.should contain(/hello, world/)
end
end
describe "asserts for contains," do
before(:each) do
should_receive(:response_body).and_return @body
require 'test/unit'
end
describe "assert_contain" do
it "should pass when containing the text" do
assert_contain("hello, world")
end
it "should pass when containing the regexp" do
assert_contain(/hello, world/)
end
it "should throw an exception when the body doesnt contain the text" do
lambda {assert_contain("monkeys")}.should raise_error(Test::Unit::AssertionFailedError)
end
it "should throw an exception when the body doesnt contain the regexp" do
lambda {assert_contain(/monkeys/)}.should raise_error(Test::Unit::AssertionFailedError)
end
end
describe "assert_not_contain" do
it "should pass when not containing the text" do
assert_not_contain("monkeys")
end
it "should pass when not containing the regexp" do
assert_not_contain(/monkeys/)
end
it "should throw an exception when the body does contain the text" do
lambda {assert_not_contain("hello, world")}.should raise_error(Test::Unit::AssertionFailedError)
end
it "should throw an exception when the body does contain the regexp" do
lambda {assert_not_contain(/hello, world/)}.should raise_error(Test::Unit::AssertionFailedError)
end
end
end
describe "#failure_message" do
it "should include the content string" do
hc = Webrat::Matchers::HasContent.new("hello, world!")
hc.matches?(@body)
hc.failure_message.should include("\"hello, world!\"")
end
it "should include the content regular expresson" do
hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/)
hc.matches?(@body)
hc.failure_message.should include("/hello,\\sworld!/")
end
it "should include the element's inner content" do
hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/)
hc.matches?(@body)
hc.failure_message.should include("hello, world!")
end
end
end
end