Skip to content
Browse files

Dup the instance of a DSL generated matcher so its state is not changed

by subsequent invocations.

- Fixes #112.
  • Loading branch information...
1 parent 40f83a9 commit 65d3c4dc000ac8192b8bcf9fc2ee96aabcd72586 @dchelimsky dchelimsky committed Jan 24, 2012
Showing with 26 additions and 6 deletions.
  1. +2 −0 Changelog.md
  2. +8 −6 lib/rspec/matchers/matcher.rb
  3. +16 −0 spec/rspec/matchers/matcher_spec.rb
View
2 Changelog.md
@@ -11,6 +11,8 @@ Bug fixes
* Align respond_to? and method_missing in DSL-defined matchers.
* Clear out user-defined instance variables between invocations of DSL-defined
matchers.
+* Dup the instance of a DSL generated matcher so its state is not changed by
+ subsequent invocations.
### 2.8.0 / 2012-01-04
View
14 lib/rspec/matchers/matcher.rb
@@ -36,14 +36,16 @@ def initialize(name, &declarations)
# @api private
def for_expected(*expected)
- instance_variables.each do |ivar|
- instance_variable_set(ivar, nil) unless PERSISENT_INSTANCE_VARIABLES.include?(ivar.intern)
- end
@expected = expected
- making_declared_methods_public do
- instance_eval_with_args(*@expected, &@declarations)
+ dup.instance_eval do
+ instance_variables.map {|ivar| ivar.intern}.each do |ivar|
+ instance_variable_set(ivar, nil) unless (PERSISENT_INSTANCE_VARIABLES + [:@expected]).include?(ivar)
+ end
+ making_declared_methods_public do
+ instance_eval_with_args(*@expected, &@declarations)
+ end
+ self
end
- self
end
# @api private
View
16 spec/rspec/matchers/matcher_spec.rb
@@ -21,6 +21,22 @@ def greeting
module RSpec::Matchers::DSL
describe Matcher do
+ it "can be stored aside and used later" do
+ # Supports using rspec-expectation matchers as argument matchers in
+ # rspec-mocks.
+ RSpec::Matchers.define :example_matcher do |expected|
+ match do |actual|
+ actual == expected
+ end
+ end
+
+ m1 = example_matcher(1)
+ m2 = example_matcher(2)
+
+ m1.matches?(1).should be_true
+ m2.matches?(2).should be_true
+ end
+
context "with an included module" do
let(:matcher) do
RSpec::Matchers::DSL::Matcher.new(:be_a_greeting) do

0 comments on commit 65d3c4d

Please sign in to comment.
Something went wrong with that request. Please try again.