Lee Longmore
2009-May-16 06:08 UTC
[rspec-users] HowTo Specify A Method is Called Within A Controller''s Action
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. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20090516/cd42ff48/attachment.html>
Phlip
2009-May-16 13:00 UTC
[rspec-users] HowTo Specify A Method is Called Within A Controller''s Action
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! -- Phlip http://flea.sourceforge.net/resume.html
David Chelimsky
2009-May-16 14:29 UTC
[rspec-users] HowTo Specify A Method is Called Within A Controller''s Action
On Sat, May 16, 2009 at 1:08 AM, Lee Longmore <lee_longmore at yahoo.co.uk> wrote:> > 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.member = stub_model(Member) controller.should_receive(:logged_in_member).and_return(member) get :action HTH, David> > Thanks, > > Lee. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >