Permalink
Browse files

Clear out user-defined instance variables between invocations of DSL-…

…defined matchers.

- Fixes #104.
  • Loading branch information...
1 parent 95bf234 commit f4efada6c86b840802a18e3de12ea68279f9030f @dchelimsky dchelimsky committed Jan 12, 2012
Showing with 24 additions and 1 deletion.
  1. +3 −1 Changelog.md
  2. +9 −0 lib/rspec/matchers/matcher.rb
  3. +12 −0 spec/rspec/matchers/dsl_spec.rb
View
@@ -8,7 +8,9 @@ Enhancements
Bug fixes
-* Align respond_to? and method_missing in matchers generated by DSL.
+* Align respond_to? and method_missing in DSL-defined matchers.
+* Clear out user-defined instance variables between invocations of DSL-defined
+ matchers.
### 2.8.0 / 2012-01-04
@@ -26,8 +26,17 @@ def initialize(name, &declarations)
}
end
+ PERSISENT_INSTANCE_VARIABLES = [
+ :@name, :@declarations, :@diffable, :@messages,
+ :@match_block, :@match_for_should_not_block,
+ :@expected_exception
+ ]
+
# @api private
def for_expected(*expected)
+ instance_variables.each do |ivar|
+ instance_variable_set(ivar, nil) unless PERSISENT_INSTANCE_VARIABLES.include?(ivar)
+ end
@expected = expected
making_declared_methods_public do
instance_eval_with_args(*@expected, &@declarations)
@@ -15,6 +15,18 @@ def question?
expect { ignore.i_dont_exist }.to raise_error(NameError)
end
+ it "clears user instance variables between invocations" do
+ RSpec::Matchers::define(:be_just_like) do |expected|
+ match do |actual|
+ @foo ||= expected
+ @foo == actual
+ end
+ end
+
+ 3.should be_just_like(3)
+ 4.should be_just_like(4)
+ end
+
describe "#respond_to?" do
it "returns true for methods in example scope" do
RSpec::Matchers::define(:ignore) {}

0 comments on commit f4efada

Please sign in to comment.