関連モデル:
class User < ActiveRecord::Base
acts_as_authentic
end
class UserSession < Authlogic::Session::Base
end
アプリケーションコントローラー:
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
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
これは User の Rspec 定義です:
FactoryGirl.define do
factory :user do
email "email@example.com"
password "password"
password_confirmation "password"
persistence_token "pertoken"
end
end
current_user
問題は、コントローラーメソッドから呼び出すたびに、常に返されることです。これは、nil
常に.UserSession.find
nil
ApplicationController
おかしなことに、(コントローラーではなく) Rspec で以下を実行すると、UserSession.find
正しく動作し、just_created_session
nil にはなりません。
UserSession.create(current_user)
just_created_session = UserSession.find
UserSession.find
したがって、問題はコントローラーで呼び出されることに固有のものです。
どんな助けでも大歓迎です。
環境
Ruby: 1.9.3p392
Rails: 3.2.12
Authlogic: 3.2.0
Factory Girl: 4.2.0
Rspec: 2.13.0
OS: Windows 7
更新:私は見ましたUserSession.create
、そしてそれがすることはこれだけです:
def create(*args, &block)
session = new(*args)
session.save(&block)
session
end
仕様から呼び出すときに戻り値を格納することさえしないため、またメソッドが格納を行っているように見えないUser.find
ため、何かを見つける方法がわかりません。