changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/spec/spec/matchers/change_spec.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1#Based on patch from Wilson Bilkovich
2
3require File.dirname(__FILE__) + '/../../spec_helper.rb'
4class SomethingExpected
5 attr_accessor :some_value
6end
7
8describe "should change(actual, message)" do
9 before(:each) do
10 @instance = SomethingExpected.new
11 @instance.some_value = 5
12 end
13
14 it "should pass when actual is modified by the block" do
15 lambda {@instance.some_value = 6}.should change(@instance, :some_value)
16 end
17
18 it "should fail when actual is not modified by the block" do
19 lambda do
20 lambda {}.should change(@instance, :some_value)
21 end.should fail_with("some_value should have changed, but is still 5")
22 end
23end
24
25describe "should_not change(actual, message)" do
26 before(:each) do
27 @instance = SomethingExpected.new
28 @instance.some_value = 5
29 end
30
31 it "should pass when actual is not modified by the block" do
32 lambda { }.should_not change(@instance, :some_value)
33 end
34
35 it "should fail when actual is not modified by the block" do
36 lambda do
37 lambda {@instance.some_value = 6}.should_not change(@instance, :some_value)
38 end.should fail_with("some_value should not have changed, but did change from 5 to 6")
39 end
40end
41
42describe "should change { block }" do
43 before(:each) do
44 @instance = SomethingExpected.new
45 @instance.some_value = 5
46 end
47
48 it "should pass when actual is modified by the block" do
49 lambda {@instance.some_value = 6}.should change { @instance.some_value }
50 end
51
52 it "should fail when actual is not modified by the block" do
53 lambda do
54 lambda {}.should change{ @instance.some_value }
55 end.should fail_with("result should have changed, but is still 5")
56 end
57
58 it "should warn if passed a block using do/end" do
59 lambda do
60 lambda {}.should change do
61 end
62 end.should raise_error(Spec::Matchers::MatcherError, /block passed to should or should_not/)
63 end
64end
65
66describe "should_not change { block }" do
67 before(:each) do
68 @instance = SomethingExpected.new
69 @instance.some_value = 5
70 end
71
72 it "should pass when actual is modified by the block" do
73 lambda {}.should_not change{ @instance.some_value }
74 end
75
76 it "should fail when actual is not modified by the block" do
77 lambda do
78 lambda {@instance.some_value = 6}.should_not change { @instance.some_value }
79 end.should fail_with("result should not have changed, but did change from 5 to 6")
80 end
81
82 it "should warn if passed a block using do/end" do
83 lambda do
84 lambda {}.should_not change do
85 end
86 end.should raise_error(Spec::Matchers::MatcherError, /block passed to should or should_not/)
87 end
88end
89
90describe "should change(actual, message).by(expected)" do
91 before(:each) do
92 @instance = SomethingExpected.new
93 @instance.some_value = 5
94 end
95
96 it "should pass when attribute is changed by expected amount" do
97 lambda { @instance.some_value += 1 }.should change(@instance, :some_value).by(1)
98 end
99
100 it "should fail when the attribute is changed by unexpected amount" do
101 lambda do
102 lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by(1)
103 end.should fail_with("some_value should have been changed by 1, but was changed by 2")
104 end
105
106 it "should fail when the attribute is changed by unexpected amount in the opposite direction" do
107 lambda do
108 lambda { @instance.some_value -= 1 }.should change(@instance, :some_value).by(1)
109 end.should fail_with("some_value should have been changed by 1, but was changed by -1")
110 end
111end
112
113describe "should change{ block }.by(expected)" do
114 before(:each) do
115 @instance = SomethingExpected.new
116 @instance.some_value = 5
117 end
118
119 it "should pass when attribute is changed by expected amount" do
120 lambda { @instance.some_value += 1 }.should change{@instance.some_value}.by(1)
121 end
122
123 it "should fail when the attribute is changed by unexpected amount" do
124 lambda do
125 lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by(1)
126 end.should fail_with("result should have been changed by 1, but was changed by 2")
127 end
128
129 it "should fail when the attribute is changed by unexpected amount in the opposite direction" do
130 lambda do
131 lambda { @instance.some_value -= 1 }.should change{@instance.some_value}.by(1)
132 end.should fail_with("result should have been changed by 1, but was changed by -1")
133 end
134end
135
136describe "should change(actual, message).by_at_least(expected)" do
137 before(:each) do
138 @instance = SomethingExpected.new
139 @instance.some_value = 5
140 end
141
142 it "should pass when attribute is changed by greater than the expected amount" do
143 lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by_at_least(1)
144 end
145
146 it "should pass when attribute is changed by the expected amount" do
147 lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by_at_least(2)
148 end
149
150 it "should fail when the attribute is changed by less than the expected amount" do
151 lambda do
152 lambda { @instance.some_value += 1 }.should change(@instance, :some_value).by_at_least(2)
153 end.should fail_with("some_value should have been changed by at least 2, but was changed by 1")
154 end
155
156end
157
158describe "should change{ block }.by_at_least(expected)" do
159 before(:each) do
160 @instance = SomethingExpected.new
161 @instance.some_value = 5
162 end
163
164 it "should pass when attribute is changed by greater than expected amount" do
165 lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by_at_least(1)
166 end
167
168 it "should pass when attribute is changed by the expected amount" do
169 lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by_at_least(2)
170 end
171
172 it "should fail when the attribute is changed by less than the unexpected amount" do
173 lambda do
174 lambda { @instance.some_value += 1 }.should change{@instance.some_value}.by_at_least(2)
175 end.should fail_with("result should have been changed by at least 2, but was changed by 1")
176 end
177end
178
179
180describe "should change(actual, message).by_at_most(expected)" do
181 before(:each) do
182 @instance = SomethingExpected.new
183 @instance.some_value = 5
184 end
185
186 it "should pass when attribute is changed by less than the expected amount" do
187 lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by_at_most(3)
188 end
189
190 it "should pass when attribute is changed by the expected amount" do
191 lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by_at_most(2)
192 end
193
194 it "should fail when the attribute is changed by greater than the expected amount" do
195 lambda do
196 lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by_at_most(1)
197 end.should fail_with("some_value should have been changed by at most 1, but was changed by 2")
198 end
199
200end
201
202describe "should change{ block }.by_at_most(expected)" do
203 before(:each) do
204 @instance = SomethingExpected.new
205 @instance.some_value = 5
206 end
207
208 it "should pass when attribute is changed by less than expected amount" do
209 lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by_at_most(3)
210 end
211
212 it "should pass when attribute is changed by the expected amount" do
213 lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by_at_most(2)
214 end
215
216 it "should fail when the attribute is changed by greater than the unexpected amount" do
217 lambda do
218 lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by_at_most(1)
219 end.should fail_with("result should have been changed by at most 1, but was changed by 2")
220 end
221end
222
223describe "should change(actual, message).from(old)" do
224 before(:each) do
225 @instance = SomethingExpected.new
226 @instance.some_value = 'string'
227 end
228
229 it "should pass when attribute is == to expected value before executing block" do
230 lambda { @instance.some_value = "astring" }.should change(@instance, :some_value).from("string")
231 end
232
233 it "should fail when attribute is not == to expected value before executing block" do
234 lambda do
235 lambda { @instance.some_value = "knot" }.should change(@instance, :some_value).from("cat")
236 end.should fail_with("some_value should have initially been \"cat\", but was \"string\"")
237 end
238end
239
240describe "should change{ block }.from(old)" do
241 before(:each) do
242 @instance = SomethingExpected.new
243 @instance.some_value = 'string'
244 end
245
246 it "should pass when attribute is == to expected value before executing block" do
247 lambda { @instance.some_value = "astring" }.should change{@instance.some_value}.from("string")
248 end
249
250 it "should fail when attribute is not == to expected value before executing block" do
251 lambda do
252 lambda { @instance.some_value = "knot" }.should change{@instance.some_value}.from("cat")
253 end.should fail_with("result should have initially been \"cat\", but was \"string\"")
254 end
255end
256
257describe "should change(actual, message).to(new)" do
258 before(:each) do
259 @instance = SomethingExpected.new
260 @instance.some_value = 'string'
261 end
262
263 it "should pass when attribute is == to expected value after executing block" do
264 lambda { @instance.some_value = "cat" }.should change(@instance, :some_value).to("cat")
265 end
266
267 it "should fail when attribute is not == to expected value after executing block" do
268 lambda do
269 lambda { @instance.some_value = "cat" }.should change(@instance, :some_value).from("string").to("dog")
270 end.should fail_with("some_value should have been changed to \"dog\", but is now \"cat\"")
271 end
272end
273
274describe "should change{ block }.to(new)" do
275 before(:each) do
276 @instance = SomethingExpected.new
277 @instance.some_value = 'string'
278 end
279
280 it "should pass when attribute is == to expected value after executing block" do
281 lambda { @instance.some_value = "cat" }.should change{@instance.some_value}.to("cat")
282 end
283
284 it "should fail when attribute is not == to expected value after executing block" do
285 lambda do
286 lambda { @instance.some_value = "cat" }.should change{@instance.some_value}.from("string").to("dog")
287 end.should fail_with("result should have been changed to \"dog\", but is now \"cat\"")
288 end
289end
290
291describe "should change(actual, message).from(old).to(new)" do
292 before(:each) do
293 @instance = SomethingExpected.new
294 @instance.some_value = 'string'
295 end
296
297 it "should pass when #to comes before #from" do
298 lambda { @instance.some_value = "cat" }.should change(@instance, :some_value).to("cat").from("string")
299 end
300
301 it "should pass when #from comes before #to" do
302 lambda { @instance.some_value = "cat" }.should change(@instance, :some_value).from("string").to("cat")
303 end
304end
305
306describe "should change{ block }.from(old).to(new)" do
307 before(:each) do
308 @instance = SomethingExpected.new
309 @instance.some_value = 'string'
310 end
311
312 it "should pass when #to comes before #from" do
313 lambda { @instance.some_value = "cat" }.should change{@instance.some_value}.to("cat").from("string")
314 end
315
316 it "should pass when #from comes before #to" do
317 lambda { @instance.some_value = "cat" }.should change{@instance.some_value}.from("string").to("cat")
318 end
319end