Forum: RSpec How to properly mock a complex object "comment.initiator.group.name"?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3d9c33db9c320c170d5593488200a911?d=identicon&s=25 Evgeny Bogdanov (Guest)
on 2009-02-25 11:15
(Received via mailing list)
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!
39100495c9937c39b2e0c704444e1b4a?d=identicon&s=25 Pat Maddox (Guest)
on 2009-02-25 11:43
(Received via mailing list)
On Wed, Feb 25, 2009 at 2:13 AM, Evgeny Bogdanov
<evgeny.bogdanov@gmail.com> wrote:
> comment.stub!(:initiator).and_return(initiator)
> rspec-users@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
3d9c33db9c320c170d5593488200a911?d=identicon&s=25 Evgeny Bogdanov (Guest)
on 2009-02-25 23:13
(Received via mailing list)
Thanks a lot!
Got an idea!
This topic is locked and can not be replied to.