forked from dchelimsky/rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
include_text.diff
185 lines (183 loc) · 11.8 KB
/
include_text.diff
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
Index: rspec_on_rails/lib/spec/rails/matchers/have_text.rb
===================================================================
--- rspec_on_rails/lib/spec/rails/matchers/have_text.rb (revision 3333)
+++ rspec_on_rails/lib/spec/rails/matchers/have_text.rb (working copy)
@@ -39,9 +39,11 @@
# Accepts a String or a Regexp, matching a String using ==
# and a Regexp using =~.
#
+ # If response_or_text has a #body, then that is used as to match against
+ # else it uses response_or_text
+ #
# Use this instead of <tt>response.should have_tag()</tt>
- # when you either don't know or don't care where on the page
- # this text appears.
+ # when you want to match the whole string or whole body
#
# == Examples
#
Index: rspec_on_rails/lib/spec/rails/matchers/include_text.rb
===================================================================
--- rspec_on_rails/lib/spec/rails/matchers/include_text.rb (revision 0)
+++ rspec_on_rails/lib/spec/rails/matchers/include_text.rb (revision 0)
@@ -0,0 +1,54 @@
+module Spec
+ module Rails
+ module Matchers
+
+ class IncludeText #:nodoc:
+
+ def initialize(expected)
+ @expected = expected
+ end
+
+ def matches?(response_or_text)
+ @actual = response_or_text.respond_to?(:body) ? response_or_text.body : response_or_text
+ return actual.include?(expected)
+ end
+
+ def failure_message
+ "expected to find #{expected.inspect} in #{actual.inspect}"
+ end
+
+ def negative_failure_message
+ "expected not to include text #{expected.inspect}"
+ end
+
+ def to_s
+ "include text #{expected.inspect}"
+ end
+
+ private
+ attr_reader :expected
+ attr_reader :actual
+
+ end
+
+
+ # :call-seq:
+ # response.should include_text(expected)
+ # response.should_not include_text(expected)
+ #
+ # Accepts a String, matching using include?
+ #
+ # Use this instead of <tt>response.should have_text()</tt>
+ # when you either don't know or don't care where on the page
+ # this text appears.
+ #
+ # == Examples
+ #
+ # response.should include_text("This text will be in the actual string")
+ def include_text(text)
+ IncludeText.new(text)
+ end
+
+ end
+ end
+end
Index: rspec_on_rails/lib/spec/rails/version.rb
===================================================================
--- rspec_on_rails/lib/spec/rails/version.rb (revision 3333)
+++ rspec_on_rails/lib/spec/rails/version.rb (working copy)
@@ -1,7 +1,7 @@
module Spec
module Rails
module VERSION #:nodoc:
- BUILD_TIME_UTC = 20080309210001
+ BUILD_TIME_UTC = 20080315225339
end
end
end
Index: rspec_on_rails/spec/rails/matchers/include_text_spec.rb
===================================================================
--- rspec_on_rails/spec/rails/matchers/include_text_spec.rb (revision 0)
+++ rspec_on_rails/spec/rails/matchers/include_text_spec.rb (revision 0)
@@ -0,0 +1,77 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe "include_text" do
+
+ describe "where target is a Regexp" do
+ it 'should should match submitted text using a regexp' do
+ string = 'foo'
+ string.should include_text(/fo*/)
+ end
+ end
+
+ describe "where target is a String" do
+ it 'should match submitted text using a string' do
+ string = 'foo'
+ string.should include_text('foo')
+ end
+
+ it 'should match if the text is contained' do
+ string = 'I am a big piece of text'
+ string.should include_text('big piece')
+ end
+
+ it 'should not match if text is not contained' do
+ string = 'I am a big piece of text'
+ string.should_not include_text('corey')
+ end
+ end
+
+end
+
+describe "include_text",
+ :type => :controller do
+ ['isolation','integration'].each do |mode|
+ if mode == 'integration'
+ integrate_views
+ end
+
+ describe "where target is a response (in #{mode} mode)" do
+ controller_name :render_spec
+
+ it "should pass with exactly matching text" do
+ post 'text_action'
+ response.should include_text("this is the text for this action")
+ end
+
+ it 'should pass with substring matching text' do
+ post 'text_action'
+ response.should include_text('text for this')
+ end
+
+ it "should pass with matching text (using Regexp)" do
+ post 'text_action'
+ response.should include_text(/is the text/)
+ end
+
+ it "should fail with matching text" do
+ post 'text_action'
+ lambda {
+ response.should include_text("this is NOT the text for this action")
+ }.should fail_with("expected \"this is NOT the text for this action\", got \"this is the text for this action\"")
+ end
+
+ it "should fail when a template is rendered" do
+ post 'some_action'
+ lambda {
+ response.should include_text("this is the text for this action")
+ }.should fail_with(/expected \"this is the text for this action\", got .*/)
+ end
+
+ it "should pass using should_not with incorrect text" do
+ post 'text_action'
+ response.should_not include_text("the accordian guy")
+ end
+ end
+ end
+end
+
Index: rspec/lib/spec/version.rb
===================================================================
--- rspec/lib/spec/version.rb (revision 3333)
+++ rspec/lib/spec/version.rb (working copy)
@@ -6,7 +6,7 @@
TINY = 3
RELEASE_CANDIDATE = nil
- BUILD_TIME_UTC = 20080309210001
+ BUILD_TIME_UTC = 20080315225339
STRING = [MAJOR, MINOR, TINY].join('.')
TAG = "REL_#{[MAJOR, MINOR, TINY, RELEASE_CANDIDATE].compact.join('_')}".upcase.gsub(/\.|-/, '_')