Skip to content
This repository
Browse code

Add spec demonstrating odd `method_missing` behavior.

I ran into this in some rspec-mocks specs but traced it back to rspec-expectations.
  • Loading branch information...
commit 0e69857c8205e934cf3150edc63f2bf3dab9283f 1 parent e2ef482
Myron Marston authored October 22, 2012

Showing 1 changed file with 31 additions and 0 deletions. Show diff stats Hide diff stats

  1. 31  spec/rspec/matchers/be_spec.rb
31  spec/rspec/matchers/be_spec.rb
@@ -46,6 +46,37 @@
46 46
       actual.should be_foo
47 47
     }.should raise_error(/aaaah/)
48 48
   end
  49
+
  50
+  it 'does not change the type of error method_missing throws for other objects' do
  51
+    # For some reason, our `method_missing` definition (for predicates matchers)
  52
+    # triggers a strange change to Object#method_missing: it changes the error
  53
+    # raised from `NoMethodError` to `NameError`.
  54
+    #
  55
+    # Note that the 3 lines below aren't strictly needed when the entire rspec-expectations
  56
+    # spec suite is run (as other examples use a predicate matcher) but it is needed
  57
+    # for when this example is run in isolation (so a predicate matcher is still used).
  58
+    obj1 = Object.new
  59
+    def obj1.blah?; true; end
  60
+    obj1.should be_blah # commenting out this line fixes the issue.
  61
+
  62
+    obj2 = Object.new
  63
+
  64
+    # These are 3 different ways to trigger method missing.
  65
+    # This first one is the one for which a different error is rasied.
  66
+    # Note, however, that the order apparently matters: if you move
  67
+    # this first one between the other two, it fixes the issue.
  68
+    expect {
  69
+      obj2.method(:method_missing).call(:not_a_method)
  70
+    }.to raise_error(NoMethodError)
  71
+
  72
+    expect {
  73
+      obj2.not_a_method
  74
+    }.to raise_error(NoMethodError)
  75
+
  76
+    expect {
  77
+      obj2.__send__(:method_missing, :not_a_method)
  78
+    }.to raise_error(NoMethodError)
  79
+  end
49 80
 end
50 81
 
51 82
 describe "should_not be_predicate" do

0 notes on commit 0e69857

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