[rspec-users] HowTo Specify A Method is Called Within A Controller's Action

Phlip phlip2005 at gmail.com
Sat May 16 09:00:59 EDT 2009

Lee Longmore wrote:

> 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?

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!


More information about the rspec-users mailing list