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?
Think about which object is receiving that
getsmethod call. It looks like you’re calling it directly onXmlParser, since that’s whatselfwould be if I understand yourinitmethod correctly.So, does this work?