[rspec-users] File.stub!

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


Hi,

I've answered my own question. I changed before :all do; to just before
do (which is the same as before :each do) and everything works fine. I'm
not overly concerned that stubbing has to occur prior to each example
method, but why is this the case? If this behaviour is intended (which
I'm guessing it is), then perhaps this should make its way into the
docs? 

Tim Watson
Technologist
BT Web21C SDK
Email:	tim.watson at bt.com
Cell:	+44 7918 711 612


-----Original Message-----
From: rspec-users-bounces at rubyforge.org
[mailto:rspec-users-bounces at rubyforge.org] On Behalf Of
tim.watson at bt.com
Sent: 27 July 2007 08:27
To: rspec-users at rubyforge.org
Subject: Re: [rspec-users] File.stub!

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


_______________________________________________
rspec-users mailing list
rspec-users at rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list