[rspec-users] Returning a StringIO object from a stub

JGailor
2009-11-11T21:56:54Z

I''m trying to stub File.new so I can return a StringIO object from it
to set some expectation and make sure the subject under test is
behaving correctly, but calling readline() on the StringIO object in
the subject always returns nil.  What''s strange is that calling read()
returns the entire set of data in the StringIO object.  Additionally,
calling readline() on the StringIO object from inside the test works
fine as well.

data = <<-DATA
 ...
DATA

faux_file = StringIO.new(data)
File.stub!(:new).and_return(faux_file)
faux_file.should_receive(:readline).exactly(x).times
Subject.new("").parse


I''m using rspec 1.2.9 (this is not testing a rails app).  Can anyone
give me any advice on why this might be?

Thanks,

Jeremy