Permalink
Browse files

Change matcher dups Enumerable before the action, supporting custom

Enumerable types like CollectionProxy in Rails.

- Closes rspec/rspec-rails#411
  • Loading branch information...
1 parent e175d8a commit ea01a6f8787564406e951108a5d6a942880c0152 @dchelimsky dchelimsky committed Oct 6, 2011
Showing with 41 additions and 1 deletion.
  1. +2 −0 Changelog.md
  2. +1 −1 lib/rspec/matchers/change.rb
  3. +38 −0 spec/rspec/matchers/change_spec.rb
View
@@ -6,6 +6,8 @@
* HaveMatcher converts argument using #to_i (Alex Bepple & Pat Maddox)
* Improved failure message for the have_xxx matcher (Myron Marston)
* HaveMatcher supports #count (Matthew Bellantoni)
+ * Change matcher dups Enumerable before the action, supporting custom
+ Enumerable types like CollectionProxy in Rails (David Chelimsky)
* Bug fixes
* Fix typo in have(n).xyz documentation (Jean Boussier)
@@ -27,7 +27,7 @@ def raise_block_syntax_error
def evaluate_value_proc
case val = @value_proc.call
- when Array, Hash
+ when Enumerable
val.dup
else
val
@@ -95,6 +95,44 @@ class SomethingExpected
end.to fail
end
end
+
+ context "with an arbitrary enumerable" do
+ before(:each) do
+ @instance = SomethingExpected.new
+ @instance.some_value = Class.new do
+ include Enumerable
+
+ attr_reader :elements
+
+ def initialize(*elements)
+ @elements = elements.dup
+ end
+
+ def <<(element)
+ elements << element
+ end
+
+ def dup
+ self.class.new *elements
+ end
+
+ def ==(other)
+ elements == other.elements
+ end
+ end.new
+ end
+
+ it "passes when actual is modified by the block" do
+ expect {@instance.some_value << 1}.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/)
+ end
+
+ end
end
describe "should_not change(actual, message)" do

0 comments on commit ea01a6f

Please sign in to comment.