Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

should[_not] change now handles boolean values correctly

- Closes #16.
  • Loading branch information...
commit ecd24f595d7e785dcc9abf60bd0489bb41f5a12d 1 parent ed7e32a
@dchelimsky dchelimsky authored
Showing with 104 additions and 45 deletions.
  1. +8 −5 lib/rspec/matchers/change.rb
  2. +96 −40 spec/rspec/matchers/change_spec.rb
View
13 lib/rspec/matchers/change.rb
@@ -7,6 +7,7 @@ def initialize(receiver=nil, message=nil, &block)
@message = message || "result"
@value_proc = block || lambda {receiver.__send__(message)}
@to = @from = @minimum = @maximum = @amount = nil
+ @given_from = @given_to = false
end
def matches?(event_proc)
@@ -16,8 +17,8 @@ def matches?(event_proc)
event_proc.call
@after = evaluate_value_proc
- return (@to = false) if @from unless @from == @before
- return false if @to unless @to == @after
+ return false if @given_from unless @from == @before
+ return false if @given_to unless @to == @after
return (@before + @amount == @after) if @amount
return ((@after - @before) >= @minimum) if @minimum
return ((@after - @before) <= @maximum) if @maximum
@@ -36,10 +37,10 @@ def evaluate_value_proc
end
def failure_message_for_should
- if @to
- "#{@message} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
- elsif @from
+ if @given_from && @before != @from
"#{@message} should have initially been #{@from.inspect}, but was #{@before.inspect}"
+ elsif @given_to && @to != @after
+ "#{@message} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
elsif @amount
"#{@message} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}"
elsif @minimum
@@ -75,11 +76,13 @@ def by_at_most(maximum)
end
def to(to)
+ @given_to = true
@to = to
self
end
def from (from)
+ @given_from = true
@from = from
self
end
View
136 spec/rspec/matchers/change_spec.rb
@@ -1,28 +1,48 @@
#Based on patch from Wilson Bilkovich
require 'spec_helper'
+
class SomethingExpected
attr_accessor :some_value
end
describe "should change(actual, message)" do
- before(:each) do
- @instance = SomethingExpected.new
- @instance.some_value = 5
+ context "with a numeric value" do
+ before(:each) do
+ @instance = SomethingExpected.new
+ @instance.some_value = 5
+ end
+
+ it "passes when actual is modified by the block" do
+ expect {@instance.some_value = 6}.to change(@instance, :some_value)
+ end
+
+ it "fails when actual is not modified by the block" do
+ expect do
+ expect {}.to change(@instance, :some_value)
+ end.to fail_with("some_value should have changed, but is still 5")
+ end
+
+ it "provides a #description" do
+ change(@instance, :some_value).description.should == "change #some_value"
+ end
end
- it "passes when actual is modified by the block" do
- expect {@instance.some_value = 6}.to change(@instance, :some_value)
- end
+ context "with boolean values" do
+ before(:each) do
+ @instance = SomethingExpected.new
+ @instance.some_value = true
+ end
- it "fails when actual is not modified by the block" do
- expect do
- expect {}.to change(@instance, :some_value)
- end.to fail_with("some_value should have changed, but is still 5")
- end
-
- it "provides a #description" do
- change(@instance, :some_value).description.should == "change #some_value"
+ it "passes when actual is modified by the block" do
+ expect {@instance.some_value = false}.to change(@instance, :some_value)
+ end
+
+ it "fails when actual is not modified by the block" do
+ expect do
+ expect {}.to change(@instance, :some_value)
+ end.to fail_with("some_value should have changed, but is still true")
+ end
end
end
@@ -227,19 +247,37 @@ class SomethingExpected
end
describe "should change(actual, message).from(old)" do
- before(:each) do
- @instance = SomethingExpected.new
- @instance.some_value = 'string'
- end
-
- it "passes when attribute is == to expected value before executing block" do
- expect { @instance.some_value = "astring" }.to change(@instance, :some_value).from("string")
- end
-
- it "fails when attribute is not == to expected value before executing block" do
- expect do
- expect { @instance.some_value = "knot" }.to change(@instance, :some_value).from("cat")
- end.to fail_with("some_value should have initially been \"cat\", but was \"string\"")
+ context "with boolean values" do
+ before(:each) do
+ @instance = SomethingExpected.new
+ @instance.some_value = true
+ end
+
+ it "passes when attribute is == to expected value before executing block" do
+ expect { @instance.some_value = false }.to change(@instance, :some_value).from(true)
+ end
+
+ it "fails when attribute is not == to expected value before executing block" do
+ expect do
+ expect { @instance.some_value = 'foo' }.to change(@instance, :some_value).from(false)
+ end.to fail_with("some_value should have initially been false, but was true")
+ end
+ end
+ context "with non-boolean values" do
+ before(:each) do
+ @instance = SomethingExpected.new
+ @instance.some_value = 'string'
+ end
+
+ it "passes when attribute is == to expected value before executing block" do
+ expect { @instance.some_value = "astring" }.to change(@instance, :some_value).from("string")
+ end
+
+ it "fails when attribute is not == to expected value before executing block" do
+ expect do
+ expect { @instance.some_value = "knot" }.to change(@instance, :some_value).from("cat")
+ end.to fail_with("some_value should have initially been \"cat\", but was \"string\"")
+ end
end
end
@@ -261,19 +299,37 @@ class SomethingExpected
end
describe "should change(actual, message).to(new)" do
- before(:each) do
- @instance = SomethingExpected.new
- @instance.some_value = 'string'
- end
-
- it "passes when attribute is == to expected value after executing block" do
- expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to("cat")
- end
-
- it "fails when attribute is not == to expected value after executing block" do
- expect do
- expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("dog")
- end.to fail_with("some_value should have been changed to \"dog\", but is now \"cat\"")
+ context "with boolean values" do
+ before(:each) do
+ @instance = SomethingExpected.new
+ @instance.some_value = true
+ end
+
+ it "passes when attribute is == to expected value after executing block" do
+ expect { @instance.some_value = false }.to change(@instance, :some_value).to(false)
+ end
+
+ it "fails when attribute is not == to expected value after executing block" do
+ expect do
+ expect { @instance.some_value = 1 }.to change(@instance, :some_value).from(true).to(false)
+ end.to fail_with("some_value should have been changed to false, but is now 1")
+ end
+ end
+ context "with non-boolean values" do
+ before(:each) do
+ @instance = SomethingExpected.new
+ @instance.some_value = 'string'
+ end
+
+ it "passes when attribute is == to expected value after executing block" do
+ expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to("cat")
+ end
+
+ it "fails when attribute is not == to expected value after executing block" do
+ expect do
+ expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("dog")
+ end.to fail_with("some_value should have been changed to \"dog\", but is now \"cat\"")
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.