[rspec-users] spec'ing the :conditions argument of a find

Matt Wynne matt at mattwynne.net
Thu Oct 30 12:41:33 EDT 2008


On 30 Oct 2008, at 15:58, Zach Dennis wrote:
> I know the above example breaks the one assertion per test guideline  
> people strive to adhere to, but I think it is ok.  If there are more  
> examples that should be used to make sure find_thingy works then I'd  
> break out a separate describe block and have multiple 'it' examples,

It also goes to the database, which will make it a slow unit test. I  
personally do pretty much the same thing myself mostly when working  
with ActiveRecord, but it doesn't mean I'm comfortable with it. (And  
it also doesn't mean our unit test suite is anything other than  
shamefully slow)

I did experiment with a QueryApapter for this purpose which has worked  
out quite well for us. You end up with code like this in the Controller:

	products = Product.find(:all, Product::QueryAdapter.new(params).adapt)

You then have a QueryAdapter class to test which is solely responsible  
for mapping web query params into database query params - so that's  
where you put the logic to build your conditions, your limit etc, and  
return them as a hash from QueryAdapter#adapt.

The family of QueryAdapters then become the one place where I'm  
coupled to ActiveRecord's find method arguments, which is nice, rather  
than having it sprinkled all over the controller code.

HTH,
Matt


More information about the rspec-users mailing list