Reputation: 13621
I'm testing an initialization method that uses both puts and gets to start off a small script.
Here's the code:
def init
puts 'type xml file name'
@xml_name = gets.chomp
f = File.open(@xml_name)
doc = Nokogiri::XML(f)
f.close
build_headers(doc)
end
Here's the test code:
describe XmlParser do
describe "init" do
before(:each) do
stub!(:gets).and_return('')
stub!(:puts)
end
it "should give a greeting message 'type xml file name'" do
XmlParser.stub!(:build_headers).with(nil)
should_receive(:puts).with('type xml file name')
XmlParser::init
end
end
end
It essentially throws an error when gets is called in the init method. Is there a way to simply stub these methods? Or should i refactor the code to use an accepted STDOUT and STDIN, and simply stub those objects?
Upvotes: 0
Views: 2092
Reputation: 4409
Think about which object is receiving that gets
method call. It looks like you're calling it directly on XmlParser
, since that's what self
would be if I understand your init
method correctly.
So, does this work?
XmlParser.stub(:gets).and_return('')
Upvotes: 2