[rspec-users] Mocking and stubbing Rails' association extensions

Chris Kampmeier chris at kampers.net
Thu Jan 10 13:35:36 EST 2008

I'm having a lot of trouble stubbing out an association extension for  
some view tests. Example rails code modeling a music album:

class Album < ActiveRecord::Base
   has_many :songs do
     def streamable
       find(:all, :conditions => 'streamable = 1')

So for a given Album instance (say @album), I need to be able to stub  
both @album.songs and @album.songs.streamable in the same before block.

Is there a way for a stub to return one thing when called alone  
(@album.songs) and another stub when the call is chained?  

Before adding the extension, I just had @album.songs returning an  
array of Song instances. The only thing I've thought of that would  
work is temporarily extending Array itself to respond to #streamable,  
but that feels ugly.

Thanks for any ideas,

Chris Kampmeier

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2417 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/rspec-users/attachments/20080110/57baee23/attachment.bin 

More information about the rspec-users mailing list