[rspec-users] How to mock an association

Bryan Ray bryansray at gmail.com
Thu Feb 14 12:11:53 EST 2008

I don't know exactly how you're using the code, but validates_presence_of
:user_id seems redundant. If you've setup a relationship from Group to User
then Active Record will be enforcing that validation for you. Just a thought

Other than that you would probably do (without seeing any of your code):

group = mock_model(Group, :to_param => 1)
user = mock_model(User, :to_param => 1)


On Mon, Feb 11, 2008 at 8:21 AM, Edvard Majakari <edvard at majakari.net>

> On Feb 11, 2008 1:43 PM, Wes Shaddix <wshaddix at gmail.com> wrote:
> > I've got a "group" model that has a user_id attribute and a
> > validates_existence_of :user and validates_presence_of :user_id
> > validations. What method(s) do I need to stub on the User mock to
> > intercept those validation calls? My goal is to isolate the Group model
> > from the User model.
> See http://www.ruby-forum.com/topic/138342
> and http://tersesystems.com/post/9700067.jhtml
> Obvious 'rails mocking associations' seemed to work for me, unless I
> misunderstood the question.
> Of course, you could just stub 'valid?' for any AR object as well.
> --
> "One day, when he was naughty, Mr Bunnsy looked over the hedge into
> Farmer Fred's field and it was full of fresh green lettuces. Mr
> Bunnsy, however, was not full of lettuces. This did not seem fair."
>  -- Terry Pratchett, Mr. Bunnsy Has An Adventure
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Bryan Ray

"Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080214/76f15584/attachment.html 

More information about the rspec-users mailing list