[rspec-users] File.stub!

tim.watson at bt.com tim.watson at bt.com
Fri Jul 27 03:27:05 EDT 2007


Hi, 

I'm trying to stub File.open and whenever I do, rspec blows up with the
following error message (undefined method `add' for nil:NilClass), which
seems to happen because method __add in class Proxy is calling #add on
global $rspec_mocks, which in turn is nil. 

Can someone explain what I'm doing wrong, as I can't seem to stub
anything out! Here's my code:

class Foo    
    def Foo.open( name, mode )
        return name
    end
end

describe Something, 'blah blah' do
    
    class FooDouble #:nodoc:
        attr_reader :data
        def initialize
            @data = []
        end
        def method_missing( name, *args )
            puts "ignoring call to #{name} with args [#{args.each |arg|
arg.inspect}]"
        end
        def <<(raw_data)
            @data.push raw_data
        end
    end

    before :all do
        Foo.stub!( :open ).and_return( FooDouble.new )
	  #have also tried and_return { FooDouble.new }
    end
   
    ...
end




More information about the rspec-users mailing list