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

JGailor jeremy at infinitecube.com
Wed Nov 11 16:56:54 EST 2009


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


More information about the rspec-users mailing list