[rspec-users] Possible bug in proxy.rb in Rspec 1.3.1

Andrew Selder aselder at mac.com
Thu Mar 3 16:43:04 EST 2011


In the proxy.rb file, Rspec 1.3.1 added a stanza to the it/else stack

        elsif @target.is_a?(Class)
          @target.superclass.send(sym, *args, &block)

This just seems wrong to me and I'm pretty sure it's causing
catastrophic errors in my build.

Consider the following example:

require 'spec_helper'

class Foo

    def self.singleton
      @singleton ||= new
    end

    # Redirect all missing class methods to the singleton instance for
backwards compatible API
    def self.method_missing(name,*args,&block)
      self.singleton.send(name,*args,&block)
    end

    def bar
      "abc"
    end
end

describe Foo do

  it "should mock correctly" do
    Foo.should_receive(:bar).and_return(123)
    Foo.bar.should == 123
  end

  it "should call successfully after a mock" do
    Foo.bar.should == "abc"
  end

end

The first example creates a message_expectation for the Foo class
object, which defines the :bar method on Foo, since as written
Foo.respond_to?(:bar) returns false. This method send the name of the
method to the __mock_proxy which delegates it to message received.

As we move to the second example, the method defined by
define_expected_method remains, which sends the message to the
mock_proxy's message_received methods. Since there are no expectations
defined, we drop to the suspect code. Since the @target of the proxy
is Foo, and Foo is a Class, the message tries to get sent to Foo's
superclass, namely Object, which doesn't define #bar and blows up.

Filed as bug #17 at http://github.com/dchelimsky/rspec/issues/17


More information about the rspec-users mailing list