[rspec-users] How to properly mock a complex object "comment.initiator.group.name"?

Evgeny Bogdanov evgeny.bogdanov at gmail.com
Wed Feb 25 17:11:39 EST 2009


Thanks a lot!
Got an idea!

On Feb 25, 11:35 am, Pat Maddox <pat.mad... at gmail.com> wrote:
> On Wed, Feb 25, 2009 at 2:13 AM, Evgeny Bogdanov
>
>
>
> <evgeny.bogda... at gmail.com> wrote:
> > I am wondering what is the best way to mock such expression:
> > "comment.initiator.group.name"
>
> > What I do now is:
> > ===========================
> > comment = mock_model(Comment)
> > initiator = mock_model(User)
> > group = mock_model(Group, :name => "Admin")
>
> > initiator.stub!(:group).and_return(group)
> > comment.stub!(:initiator).and_return(initiator)
> > ===========================
> > So it becomes quite complex when the length of the expression
> > increases.
>
> > Is it possible to do something like this:
> > controller.stub!(:comment.initiator.group.name).and_return("Admin")?
>
> > Thank you in advance!
> > _______________________________________________
> > rspec-users mailing list
> > rspec-us... at rubyforge.org
> >http://rubyforge.org/mailman/listinfo/rspec-users
>
> Fix the trainwreck.  An unimaginative solution would be
> comment.initiator_group_name.  Try to find an intention- rather than
> structure-revealing name.
>
> Pat
> _______________________________________________
> rspec-users mailing list
> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list