Skip to content

Instantly share code, notes, and snippets.

@tobyclemson
Created August 28, 2008 09:53
Show Gist options
  • Save tobyclemson/7693 to your computer and use it in GitHub Desktop.
Save tobyclemson/7693 to your computer and use it in GitHub Desktop.
require 'open-uri'
require 'fastercsv'
class ZoneFinder
# Creates a ZoneFinder object
def initialize(network_data_path)
@network_data_path = network_data_path
end
# Returns the network data parsed from a csv file as an array of
# FasterCSV rows.
def fetch_network_data
return FasterCSV.parse(
open(@network_data_path).read.gsub('"', '\''),
:headers => true
)
end
end
describe ZoneFinder, "#fetch_network_data" do
before(:each) do
@network_data_path = mock('network data path')
@network_data_handle = mock('network data handle')
@network_data_csv = mock('network data csv')
@network_data = mock('network data')
@network_data_handle.stub!(:read).and_return(@network_data_csv)
@network_data_csv.stub!(:gsub).and_return(@network_data_csv)
FasterCSV.stub!(:parse).and_return(@network_data)
@zone_finder = ZoneFinder.new(@network_data_path)
@zone_finder.stub!(:open).and_return(@network_data_handle)
end
it "should attempt to open the file located at the network_data_path" do
@zone_finder.should_receive(
:open
).with(
@network_data_path
).and_return(
@network_data_handle
)
@zone_finder.fetch_network_data
end
it "should read the data from the network data file" do
@network_data_handle.should_receive(:read).and_return(@network_data_csv)
@zone_finder.fetch_network_data
end
it "should replace double quotes with single quotes in the returned csv " +
"document" do
@network_data_csv.should_receive(
:gsub
).with(
'"', "'"
).and_return(
@network_data_csv
)
@zone_finder.send(:fetch_network_data)
end
it "should parse the returned csv document and return it" do
FasterCSV.should_receive(
:parse
).with(
@network_data_csv, :headers => true
).and_return(
@network_data
)
@zone_finder.send(:fetch_network_data).should == @network_data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment