[rspec-users] Stubbing authentication across multiple controllers

Blake Pettersson blake.pettersson at hotmail.com
Wed May 16 21:22:13 EDT 2007


I'm new to RSpec, so I'm not too sure of the best practises in stubbing. I was thinking along the lines of including a helper method that would stub out a users id and posts to sessions/create. It works fine when speccing SessionsController, but of course it fails when it is included into other controllers specs, because of the post. I've tried posting to an absolute path ("http://test.host/sessions/create") and to a relative path ("/sessions/create"), but that doesn't seem to work either. 

Perhaps I'm going about this the wrong way, so if anyone has any suggestions it would be appreciated.

#Helper method that is included in controller specs
def login_as(username, password = "foo", id = "1")
  user = mock(username)
  User.should_receive(:authenticate).with(username, password).and_return(true)

  #Here it fails if included in a controller that isn't SessionController
  post(:create, :login => username, :password => password)

  puts response.inspect

  controller.send :current_user=, user
Explore the seven wonders of the world

More information about the rspec-users mailing list