[rspec-users] Where is current_user?

David Chelimsky dchelimsky at gmail.com
Mon Oct 20 09:08:39 EDT 2008


On Mon, Oct 20, 2008 at 8:02 AM, Harry Bishop <lists at ruby-forum.com> wrote:
> David Chelimsky wrote:
>> On Mon, Oct 20, 2008 at 7:30 AM, Harry Bishop <lists at ruby-forum.com>
>> wrote:
>>> @current_user is retrieved in the application controller with
>>> retrieve_user.
>>
>> I don't see where retrieve_user is getting called in the rspec example
>> code or in the show action. Maybe it's not actually getting called
>> anywhere?
>
> I guess that is what's happening, although I have this line in the
> before(:each)
>
> controller.stub!(:retrieve_user).and_return(@current_user)
>
> and the show action has a :login_required which calls :logged_in?
> since MotionsController is a subclass of ApplicationController doesn't
> running the rspec test invoke the methods here:
>
> class ApplicationController < ActionController::Base
>  helper :all # include all helpers, all the time
>  before_filter :retrieve_user
>
>  protected
>
>  def retrieve_user
>    return unless session[:user_id]
>    @current_user = Person.current_auth_record(session[:user_id])
>  end

retrieve_user, the real method, sets an instance variable that other
methods expect to be set rather than returning a value. When this is
stubbed with a *return value* of the user, the instance variable never
gets set inside the controller.

I'd add a current_user method that returns @current_user, and then
stub *that* in the code examples:

# in ApplicationController

def current_user
  @current_user
end

# in MotionsController

def show
  ...
  if is_showable?(current_user, @motion)
  ...
end

# in example

controller.stub!(:current_user).and_return(@current_user)

HTH,
David



>
>  def logged_in?
>    @current_user.is_a?(Person)
>  end
>  helper_method :logged_in?
>
>  def login_required
>    return true if logged_in?
>    session[:return_to] = request.request_uri
>    redirect_to new_session_path and return false
>  end
>
> end
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list