HowTo Specify A Method is Called Within A Controller's Action


#1

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.


#2

Lee L. 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


#3

On Sat, May 16, 2009 at 1:08 AM, Lee L. removed_email_address@domain.invalid
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