[rspec-users] Help with Writing Meaningful Specs

Tom Stuart tom at experthuman.com
Tue May 27 08:04:23 EDT 2008

On 27 May 2008, at 12:44, andypearson wrote:
>    xml = REXML::Document.new Net::HTTP.get(URI.parse(feed.url))

It won't make the problem go away, but you can certainly reduce the  
blizzard of intermediate stubs by pulling this chain out into its own  
method (e.g. fetch_xml_from_url) and stubbing that method once in your  
cache_all spec.

> it has led to an awful amount of mocks and stubs, and I am not even  
> sure whether they
> are asking the correct things of the method.
> Can anyone give me some pointers on how to write useful, meaningful  
> specs
> for this method?

What part of its behaviour do you care about? (Refactoring into more  
fine-grained methods might help again here.) It seems as though all  
you're interested in is that 1. the feed URLs get fetched from the  
database, 2. the feed contents get fetched from the URLs, and 3.  
Item.prepare_and_save gets called for each item in each feed. So,  
ideally, that's what your specs for this method should be saying.

> The other thing I have found is that I seem to have incorrectly  
> stubbed
> xml.elements.each meaning that the contents of the block are never  
> called,
> how should I be specifying this behavior?

Use #and_yield, not #and_return. (And you might as well use an actual  
mock, rather than [@element], since you're stubbing the only method  
you plan to call on it.)


More information about the rspec-users mailing list