/
contain_spec.rb
118 lines (95 loc) · 3.3 KB
/
contain_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
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
describe "contain" do
include Webrat::Matchers
before(:each) do
with_html <<-HTML
<html>
<div id='main'>
<div class='inner'>hello, world!</div>
<div class='another'>hello ladies</div>
</div>
</html>
HTML
end
describe "#matches?" do
it "should call element#contains? when the argument is a string" do
webrat_session.response_body.should contain("hello, world!")
end
it "should call element#matches? when the argument is a regular expression" do
webrat_session.response_body.should contain(/hello, world/)
end
it "should treat newlines as spaces" do
"<div>it takes\ndifferent strokes</div>".should contain("it takes different strokes")
end
it "should multiple spaces as a single space" do
"<div>it takes different strokes</div>".should contain("it takes different strokes")
end
end
describe "asserts for contains," do
require 'test/unit'
include Test::Unit::Assertions
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 respect current dom" do
within ".inner" do
assert_contain("hello, world")
end
end
it "should throw an exception when the body doesnt contain the text" do
lambda {
assert_contain("monkeys")
}.should raise_error(AssertionFailedError)
end
it "should throw an exception when the body doesnt contain the regexp" do
lambda {
assert_contain(/monkeys/)
}.should raise_error(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 respect current dom" do
within ".another" do
assert_not_contain("hello, world")
end
end
it "should throw an exception when the body does contain the text" do
lambda {
assert_not_contain("hello, world")
}.should raise_error(AssertionFailedError)
end
it "should throw an exception when the body does contain the regexp" do
lambda {
assert_not_contain(/hello, world/)
}.should raise_error(AssertionFailedError)
end
end
end
describe "#failure_message" do
it "should include the content string" do
hc = Webrat::Matchers::HasContent.new("hello, world!")
hc.matches?(webrat_session.response_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?(webrat_session.response_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?(webrat_session.response_body)
hc.failure_message.should include("hello, world!")
end
end
end