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