<div><div><div>Hello generous coders,</div><div><br class="webkit-block-placeholder"></div><div>I&#39;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&#39;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>&nbsp;&nbsp;protected</div><div><br class="webkit-block-placeholder"></div><div>&nbsp;&nbsp;def geocode_address
</div><div>&nbsp;&nbsp; &nbsp;key = &quot;mykey&quot;</div><div>&nbsp;&nbsp; &nbsp;url = &quot;<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>&#39; &#39;, &#39;+&#39;)},+#{
self.postcode},+FRANCE&amp;output=xml&amp;key=#{key}&quot;</div><div>&nbsp;&nbsp; &nbsp;doc = Hpricot.XML(Iconv.iconv(&quot;UTF-8&quot;, &quot;ISO-8859-2&quot;, open(URI.escape(url)).read).to_s)</div><div>&nbsp;&nbsp; &nbsp;section = (doc/&quot;Placemark&quot;).first
</div><div>&nbsp;&nbsp; &nbsp;self.lat, self.lng = (section/&quot;coordinates&quot;).text.split(&#39;,&#39;)</div><div>&nbsp;&nbsp; &nbsp;self.town = (section/&quot;LocalityName&quot;).text</div><div>&nbsp;&nbsp; &nbsp;self.address_1 = (section/&quot;ThoroughfareName&quot;).text&nbsp;
</div><div>&nbsp;&nbsp;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>&nbsp;&nbsp;directions = &quot;q=71+av+Parmentier,+75011,+FRANCE&amp;output=xml&amp;key=mykey&quot;</div><div>&nbsp;&nbsp;xml = File.read(RAILS_ROOT + &quot;/spec/fixtures/feeds/75001.xml&quot;)</div><div>&nbsp;&nbsp;@
address.should_receive(:open).exactly(1).times.</div><div>&nbsp;&nbsp; &nbsp;with(&quot;<a href="http://maps.google.com/maps/geo?#{directions}">http://maps.google.com/maps/geo?#{directions}</a>&quot;).</div><div>&nbsp;&nbsp; &nbsp;and_return(xml)&nbsp;</div>
<div>end</div><div><br class="webkit-block-placeholder"></div><div>.......................</div><div><br class="webkit-block-placeholder"></div><div>describe Address, &quot;fetch and geocode&quot; do</div><div>&nbsp;&nbsp;include AddressSpecHelper &nbsp;
</div><div>&nbsp;</div><div>&nbsp;&nbsp;before do&nbsp;</div><div>&nbsp;&nbsp; &nbsp;@address = Address.new &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;mock_feed</div><div>&nbsp;&nbsp; &nbsp;@address.attributes = valid_address_attributes &nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;@address.save &nbsp; &nbsp;</div><div>&nbsp;&nbsp;end</div><div>
&nbsp;&nbsp;</div><div>&nbsp;&nbsp;it &quot;should populate town and address 1&quot; do</div><div>&nbsp;&nbsp; &nbsp;@address.town.should eql(&quot;Paris&quot;)</div><div>&nbsp;&nbsp; &nbsp;@address.address_1.should eql(&quot;71, Avenue Parmentier&quot;)</div><div>&nbsp;&nbsp;end</div>
<div>&nbsp;&nbsp;</div><div>&nbsp;&nbsp;it &quot;should populate lat and lng as BigDecimals&quot; do</div><div>&nbsp;&nbsp; &nbsp;@address.lat.should eql(BigDecimal(&quot;2.375258&quot;))</div><div>&nbsp;&nbsp; &nbsp;@address.lng.should eql(BigDecimal(&quot;48.864315&quot;)) &nbsp; &nbsp;
</div><div>&nbsp;&nbsp;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 &#39;Address fetch and geocode should populate town and address 1&#39;
</div><div>undefined method `read&#39; for #&lt;String:0x32a0370&gt;</div><div><br class="webkit-block-placeholder"></div><div>1)</div><div>NoMethodError in &#39;Address fetch and geocode should populate lat and lng as BigDecimals&#39;
</div><div>undefined method `read&#39; for #&lt;String:0x32b6d50&gt;</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> &nbsp;key = &quot;mykey&quot;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;url = &quot;<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>&#39; &#39;, &#39;+&#39;)},+#{self.postcode},+FRANCE&amp;output=xml&amp;key=#{key}&quot;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;doc = 
Hpricot.XML(open(url))</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;self.lat, self.lng = (doc/&quot;coordinates&quot;).text.split(&#39;,&#39;)</div><div><span class="Apple-tab-span" style="white-space:pre">
        </span> &nbsp;self.town = (doc/&quot;LocalityName&quot;).text</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp;self.address_1 = (doc/&quot;ThoroughfareName&quot;).text&nbsp;</div><div><span class="Apple-tab-span" style="white-space:pre">
        </span>end</div><div><br class="webkit-block-placeholder"></div><div>I&#39;m new to rspec and the ideas behind mocks and stubs. I don&#39;t understand why rspec is&nbsp;</div><div>paying any notice to &#39;read&#39;. Do I also need to inform the mock of the &#39;read&#39; 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>