Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…defined matchers.

- Fixes #104.
  • Loading branch information...
commit f4efada6c86b840802a18e3de12ea68279f9030f 1 parent 95bf234
@dchelimsky dchelimsky authored
View
4 Changelog.md
@@ -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
View
9 lib/rspec/matchers/matcher.rb
@@ -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)
View
12 spec/rspec/matchers/dsl_spec.rb
@@ -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) {}
Please sign in to comment.
Something went wrong with that request. Please try again.