1 #Based on patch from Wilson Bilkovich
3 require File.dirname(__FILE__) + '/../../spec_helper.rb'
4 class SomethingExpected
5 attr_accessor :some_value
8 describe "should change(actual, message)" do
10 @instance = SomethingExpected.new
11 @instance.some_value = 5
14 it "should pass when actual is modified by the block" do
15 lambda {@instance.some_value = 6}.should change(@instance, :some_value)
18 it "should fail when actual is not modified by the block" do
20 lambda {}.should change(@instance, :some_value)
21 end.should fail_with("some_value should have changed, but is still 5")
25 describe "should_not change(actual, message)" do
27 @instance = SomethingExpected.new
28 @instance.some_value = 5
31 it "should pass when actual is not modified by the block" do
32 lambda { }.should_not change(@instance, :some_value)
35 it "should fail when actual is not modified by the block" 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")
42 describe "should change { block }" do
44 @instance = SomethingExpected.new
45 @instance.some_value = 5
48 it "should pass when actual is modified by the block" do
49 lambda {@instance.some_value = 6}.should change { @instance.some_value }
52 it "should fail when actual is not modified by the block" do
54 lambda {}.should change{ @instance.some_value }
55 end.should fail_with("result should have changed, but is still 5")
58 it "should warn if passed a block using do/end" do
60 lambda {}.should change do
62 end.should raise_error(Spec::Matchers::MatcherError, /block passed to should or should_not/)
66 describe "should_not change { block }" do
68 @instance = SomethingExpected.new
69 @instance.some_value = 5
72 it "should pass when actual is modified by the block" do
73 lambda {}.should_not change{ @instance.some_value }
76 it "should fail when actual is not modified by the block" 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")
82 it "should warn if passed a block using do/end" do
84 lambda {}.should_not change do
86 end.should raise_error(Spec::Matchers::MatcherError, /block passed to should or should_not/)
90 describe "should change(actual, message).by(expected)" do
92 @instance = SomethingExpected.new
93 @instance.some_value = 5
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)
100 it "should fail when the attribute is changed by unexpected amount" 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")
106 it "should fail when the attribute is changed by unexpected amount in the opposite direction" 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")
113 describe "should change{ block }.by(expected)" do
115 @instance = SomethingExpected.new
116 @instance.some_value = 5
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)
123 it "should fail when the attribute is changed by unexpected amount" 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")
129 it "should fail when the attribute is changed by unexpected amount in the opposite direction" 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")
136 describe "should change(actual, message).by_at_least(expected)" do
138 @instance = SomethingExpected.new
139 @instance.some_value = 5
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)
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)
150 it "should fail when the attribute is changed by less than the expected amount" 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")
158 describe "should change{ block }.by_at_least(expected)" do
160 @instance = SomethingExpected.new
161 @instance.some_value = 5
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)
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)
172 it "should fail when the attribute is changed by less than the unexpected amount" 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")
180 describe "should change(actual, message).by_at_most(expected)" do
182 @instance = SomethingExpected.new
183 @instance.some_value = 5
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)
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)
194 it "should fail when the attribute is changed by greater than the expected amount" 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")
202 describe "should change{ block }.by_at_most(expected)" do
204 @instance = SomethingExpected.new
205 @instance.some_value = 5
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)
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)
216 it "should fail when the attribute is changed by greater than the unexpected amount" 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")
223 describe "should change(actual, message).from(old)" do
225 @instance = SomethingExpected.new
226 @instance.some_value = 'string'
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")
233 it "should fail when attribute is not == to expected value before executing block" 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\"")
240 describe "should change{ block }.from(old)" do
242 @instance = SomethingExpected.new
243 @instance.some_value = 'string'
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")
250 it "should fail when attribute is not == to expected value before executing block" 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\"")
257 describe "should change(actual, message).to(new)" do
259 @instance = SomethingExpected.new
260 @instance.some_value = 'string'
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")
267 it "should fail when attribute is not == to expected value after executing block" 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\"")
274 describe "should change{ block }.to(new)" do
276 @instance = SomethingExpected.new
277 @instance.some_value = 'string'
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")
284 it "should fail when attribute is not == to expected value after executing block" 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\"")
291 describe "should change(actual, message).from(old).to(new)" do
293 @instance = SomethingExpected.new
294 @instance.some_value = 'string'
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")
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")
306 describe "should change{ block }.from(old).to(new)" do
308 @instance = SomethingExpected.new
309 @instance.some_value = 'string'
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")
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")