Skip to content

Commit

Permalink
Fix method_missing so that we don't get StackOverflow error when RSpe…
Browse files Browse the repository at this point in the history
…c::Matchers is included via config.include.
  • Loading branch information
myronmarston committed Mar 4, 2011
1 parent 80e5300 commit cedf51d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/rspec/matchers/method_missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ module Matchers
# elegant, but it fixes the issue.
def self.included(base)
base.class_eval do
alias_method :method_missing_without_rspec_matchers, :method_missing
alias_method :method_missing, :method_missing_with_rspec_matchers
unless instance_methods.map { |m| m.to_s }.include?("method_missing_with_rspec_matchers")
include MethodMissing
alias_method :method_missing_without_rspec_matchers, :method_missing
alias_method :method_missing, :method_missing_with_rspec_matchers
end
end
end

def method_missing_with_rspec_matchers(method, *args, &block) # :nodoc:
return Matchers::BePredicate.new(method, *args, &block) if method.to_s =~ /^be_/
return Matchers::Has.new(method, *args, &block) if method.to_s =~ /^have_/
method_missing_without_rspec_matchers(method, *args, &block)
module MethodMissing
def method_missing_with_rspec_matchers(method, *args, &block) # :nodoc:
return Matchers::BePredicate.new(method, *args, &block) if method.to_s =~ /^be_/
return Matchers::Has.new(method, *args, &block) if method.to_s =~ /^have_/
method_missing_without_rspec_matchers(method, *args, &block)
end
end
end
end
8 changes: 8 additions & 0 deletions spec/rspec/matchers/method_missing_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require 'spec_helper'

RSpec.configure do |c|
c.include RSpec::Matchers, :include_rspec_matchers_an_extra_time => true
end

describe "method_missing" do
shared_examples_for "a well-behaved method_missing hook" do
it "allows undefined methods to raise errors as normal" do
Expand All @@ -18,4 +22,8 @@
include Module.new { include RSpec::Matchers }
it_behaves_like "a well-behaved method_missing hook"
end

context "when a group is configured to include RSpec::Matchers an extra time", :include_rspec_matchers_an_extra_time => true do
it_behaves_like "a well-behaved method_missing hook"
end
end

0 comments on commit cedf51d

Please sign in to comment.