Hello generous coders,
I’m having difficulty getting a couple of tests to pass.
I have an address model which takes an address and with a callback on
save
and update passes the address to google’s
geocoding service. The returned XML is parsed and saved to the address
object:
protected
def geocode_address
key = “mykey”
url = “http://maps.google.com/maps/geo?q=#{self.address_1.gsub(’ ',
‘+’)},+#{self.postcode},+FRANCE&output=xml&key=#{key}”
doc = Hpricot.XML(Iconv.iconv(“UTF-8”, “ISO-8859-2”, open(URI.escape
(url)).read).to_s)
section = (doc/“Placemark”).first
self.lat, self.lng = (section/“coordinates”).text.split(‘,’)
self.town = (section/“LocalityName”).text
self.address_1 = (section/“ThoroughfareName”).text
end
In my spec for the address model I have as a helper:
def mock_feed
directions = “q=71+av+Parmentier,+75011,+FRANCE&output=xml&key=mykey”
xml = File.read(RAILS_ROOT + “/spec/fixtures/feeds/75001.xml”)
@address.should_receive(:open).exactly(1).times.
with(“http://maps.google.com/maps/geo?#{directions}”).
and_return(xml)
end
…
describe Address, “fetch and geocode” do
include AddressSpecHelper
before do
@address = Address.new
mock_feed
@address.attributes = valid_address_attributes
@address.save
end
it “should populate town and address 1” do
@address.town.should eql(“Paris”)
@address.address_1.should eql(“71, Avenue Parmentier”)
end
it “should populate lat and lng as BigDecimals” do
@address.lat.should eql(BigDecimal(“2.375258”))
@address.lng.should eql(BigDecimal(“48.864315”))
end
end
My two errors:
NoMethodError in ‘Address fetch and geocode should populate town and
address
1’
undefined method `read’ for #String:0x32a0370
NoMethodError in ‘Address fetch and geocode should populate lat and lng
as
BigDecimals’
undefined method `read’ for #String:0x32b6d50
These tests passed fine with a previous version of the geocode_address
method:
protected
def geocode_address
key = “mykey”
url = “http://maps.google.com/maps/geo?q=#{self.address_1.gsub(’ ',
‘+’)},+#{self.postcode},+FRANCE&output=xml&key=#{key}”
doc = Hpricot.XML(open(url))
self.lat, self.lng = (doc/“coordinates”).text.split(‘,’)
self.town = (doc/“LocalityName”).text
self.address_1 = (doc/“ThoroughfareName”).text
end
I’m new to rspec and the ideas behind mocks and stubs. I don’t
understand
why rspec is
paying any notice to ‘read’. Do I also need to inform the mock of the
‘read’
method? If so, how and why?
Any pointers would be gratefully received.
Many thanks
O