Skip to content
This repository
  • 1 commit
  • 2 files changed
  • 0 comments
  • 1 contributor
Mar 04, 2011
Myron Marston Fix method_missing so that we don't get StackOverflow error when RSpe…
…c::Matchers is included via config.include.
cedf51d
17  lib/rspec/matchers/method_missing.rb
@@ -21,15 +21,20 @@ module Matchers
21 21
     # elegant, but it fixes the issue.
22 22
     def self.included(base)
23 23
       base.class_eval do
24  
-        alias_method :method_missing_without_rspec_matchers, :method_missing
25  
-        alias_method :method_missing, :method_missing_with_rspec_matchers
  24
+        unless instance_methods.map { |m| m.to_s }.include?("method_missing_with_rspec_matchers")
  25
+          include MethodMissing
  26
+          alias_method :method_missing_without_rspec_matchers, :method_missing
  27
+          alias_method :method_missing, :method_missing_with_rspec_matchers
  28
+        end
26 29
       end
27 30
     end
28 31
 
29  
-    def method_missing_with_rspec_matchers(method, *args, &block) # :nodoc:
30  
-      return Matchers::BePredicate.new(method, *args, &block) if method.to_s =~ /^be_/
31  
-      return Matchers::Has.new(method, *args, &block) if method.to_s =~ /^have_/
32  
-      method_missing_without_rspec_matchers(method, *args, &block)
  32
+    module MethodMissing
  33
+      def method_missing_with_rspec_matchers(method, *args, &block) # :nodoc:
  34
+        return Matchers::BePredicate.new(method, *args, &block) if method.to_s =~ /^be_/
  35
+        return Matchers::Has.new(method, *args, &block) if method.to_s =~ /^have_/
  36
+        method_missing_without_rspec_matchers(method, *args, &block)
  37
+      end
33 38
     end
34 39
   end
35 40
 end
8  spec/rspec/matchers/method_missing_spec.rb
... ...
@@ -1,5 +1,9 @@
1 1
 require 'spec_helper'
2 2
 
  3
+RSpec.configure do |c|
  4
+  c.include RSpec::Matchers, :include_rspec_matchers_an_extra_time => true
  5
+end
  6
+
3 7
 describe "method_missing" do
4 8
   shared_examples_for "a well-behaved method_missing hook" do
5 9
     it "allows undefined methods to raise errors as normal" do
@@ -18,4 +22,8 @@
18 22
     include Module.new { include RSpec::Matchers }
19 23
     it_behaves_like "a well-behaved method_missing hook"
20 24
   end
  25
+
  26
+  context "when a group is configured to include RSpec::Matchers an extra time", :include_rspec_matchers_an_extra_time => true do
  27
+    it_behaves_like "a well-behaved method_missing hook"
  28
+  end
21 29
 end

No commit comments for this range

Something went wrong with that request. Please try again.