[rspec-users] Mocking and stubbing model relationships

Rahoul Baruah baz at madeofstone.net
Tue Nov 18 07:33:07 EST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On 18 Nov 2008, at 05:41, Nick Hoffman wrote:

> Before writing specs for a one-to-many relationship between two  
> models, I did some research and found that some people were creating  
> proxy mocks, and others were using Matthew Heidemann's  
> #stub_association! (which essentially does that for, but in a nice,  
> DRY way):
>  http://www.ruby-forum.com/topic/126993
>
> Are those two methods currently the accepted "best practice" for  
> mocking and stubbing calls such like these?:
>
> @properties = @user.properties
> @property = @user.properties.new
> @property = @user.properties.find_by_id params[:id]


There's also Luke Redpath's Demeter's Revenge plugin (http://www.lukeredpath.co.uk/2007/10/18/demeters-revenge 
  or git cloned to http://github.com/caius/demeters_revenge/tree/ 
master).

This means you have to change your implementation code as well, but  
lets you write:

@properties = @user.properties
@property = @user.build_property # or @user.create_property
@found_properties = @user.find_properties(:conditions => { :some  
=> :values })
@user.has_no_properties? # @user.properties.empty?
@user.has_properties? # !@user.properties.empty?
and a few others.

Of course, this also makes it very easy to stub the methods for each  
association.

Baz.

Rahoul Baruah
Web design and development: http://www.3hv.co.uk/
Nottingham Forest: http://www.eighteensixtyfive.co.uk/
Serious Rails Hosting: http://www.brightbox.co.uk/
Lifecast: http://www.madeofstone.net/






-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkkitgMACgkQu0BNRvjN8xTmvwCZAZt2nQTv862fHh/8k0rfqW7p
wJIAnAikW7RStJ8OCoV2eWYqs7voz5Py
=tXFK
-----END PGP SIGNATURE-----


More information about the rspec-users mailing list