简体   繁体   中英

Authlogic/Rspec/Rails 3.2: UserSession.find returns nil in ApplicationController

Relevant models:

class User < ActiveRecord::Base 
    acts_as_authentic
end

class UserSession < Authlogic::Session::Base
end

ApplicationController:

class ApplicationController < ActionController::Base
    helper :all
    protect_from_forgery
    helper_method :current_user_session, :current_user

    private

    def current_user_session
        return @current_user_session if defined?(@current_user_session)
        @current_user_session = UserSession.find
    end

    def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.record
    end
end

Here is the Rspec:

describe "Rate Function" do
    include Authlogic::TestCase
    before(:each) do
        current_user = FactoryGirl.create(:user, persistence_token: "pt", email: "new@example.com", password: 'password', password_confirmation: 'password')
        activate_authlogic
        UserSession.create(current_user)
    end
    it "Some test for rating..." do
        get "/reviews/rate", {:format => :json, :vehicle_id => 3}
        # other stuff here, doesn't matter what it is because it never gets here
    end
    after(:each) do
    end
end

This is the Rspec definition of User:

FactoryGirl.define do
    factory :user do
        email "email@example.com"
        password "password"
        password_confirmation "password"
        persistence_token "pertoken"
    end
end

The problem is that every time I invoke current_user from any controller method, it always returns nil and that's because UserSession.find always returns nil in the ApplicationController .

Funnily enough, if I run the following in the Rspec (not in a controller), the UserSession.find works properly and just_created_session is not nil.

UserSession.create(current_user)
just_created_session = UserSession.find

So the problem is specific to UserSession.find being called in a controller.

Any help is appreciated.

Environment

Ruby: 1.9.3p392
Rails: 3.2.12
Authlogic: 3.2.0
Factory Girl: 4.2.0
Rspec: 2.13.0
OS: Windows 7

Update: I looked at UserSession.create , and all it does is this:

def create(*args, &block)
    session = new(*args)
    session.save(&block)
    session
end

Since I don't even store the return value when calling from the spec, nor does the method seem to be doing any storing, I'm not sure how we expect User.find to find anything.

I stumbled upon a similar problem with UserSession.find returning nil in the controller when driven from RSpec request specs.

Running the database cleaner in between RSpec tests caused authlogic's UserSession.find to (sometimes) return nil, even though the session was valid. I would UserSession.create(model) and immediately, UserSession.find would return nil. But only from the request specs. This was frustrating.

However, it was not authlogic's fault. I discovered a memoization in the model class which was surviving the database cleaning. This maintained a phantom reference to an active record object, and this reference, accidently passed to UserSession.create, caused the symptom. The session was created, and immediately, unfindable, but only when the database cleaner ran.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM