Forum: RSpec HowTo Specify A Method is Called Within A Controller's Action

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.
05748a61a1b05130ec508434301aecf1?d=identicon&s=25 Lee Longmore (Guest)
on 2009-05-16 08:13
(Received via mailing list)
I am new to RSpec and would appreciate some pointers on how to specify
the following for my Rails application...

I have a controller called ContextsController and, within this, the
usual 'create' method. The first line of this method should be:

@member = logged_in_member

where logged_in_member is a method that, for simplicity's sake, is a
private method of the ContextsController and retrieves a Member model
object based on the user_id in the session. I haven't written this
method yet so would like to stub it out for now.

What might an Example look like to spec this expected behaviour?

In particular, I can't work out how to specify that logged_in_member is
called.

Thanks,

Lee.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-05-16 16:45
(Received via mailing list)
Lee Longmore wrote:

> What might an Example look like to spec this expected behaviour?
If you didn't have that line yet (recommended, because test-first makes
this
stuff very easy), then what missing behavior would you see in the
resulting
page? Would it behave as if the user were logged out?

Get that behavior going, then specify it didn't happen, and that the
correct
behavior did.

If you merely test that logged_in_member got called, and if it caught a
bug, the
spec you envision would simply continue to report it was called.
Behavior Driven
Development is about specifying behaviors at a slightly higher level
than their
raw source lines!

Also, why isn't logged_in_member called from a "before" action? And

I would also build a scratch Rails site and throw the "salted
authentication"
plugin at it. That generates a very nice set of unit tests which show
how things
like this are done, IIRC!

--
   Phlip
   http://flea.sourceforge.net/resume.html
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-16 16:58
(Received via mailing list)
On Sat, May 16, 2009 at 1:08 AM, Lee Longmore <lee_longmore@yahoo.co.uk>
wrote:
> method of the ContextsController and retrieves a Member model object based
> on the user_id in the session. I haven't written this method yet so would
> like to stub it out for now.
>
> What might an Example look like to spec this expected behaviour?
>
> In particular, I can't work out how to specify that logged_in_member is
> called.

member = stub_model(Member)
controller.should_receive(:logged_in_member).and_return(member)
get :action

HTH,
David
This topic is locked and can not be replied to.