title: Authlogic/Rspec/Rails 3.2: UserSession.find returns nil in ApplicationController
body: <p>Relevant models:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base 
      acts_as_authentic
  end
  
  class UserSession &lt; Authlogic::Session::Base
  end
  </code></pre>
  
  <p>ApplicationController:</p>
  
  <pre><code>class ApplicationController &lt; 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 &amp;&amp; current_user_session.record
      end
  end
  </code></pre>
  
  <p>Here is the Rspec:</p>
  
  <pre><code>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 =&gt; :json, :vehicle_id =&gt; 3}
          # other stuff here, doesn't matter what it is because it never gets here
      end
      after(:each) do
      end
  end
  </code></pre>
  
  <p>This is the Rspec definition of User:</p>
  
  <pre><code>FactoryGirl.define do
      factory :user do
          email "email@example.com"
          password "password"
          password_confirmation "password"
          persistence_token "pertoken"
      end
  end
  </code></pre>
  
  <p>The problem is that every time I invoke <code>current_user</code> from any controller method, it always returns <code>nil</code> and that's because <code>UserSession.find</code> always returns <code>nil</code> in the <code>ApplicationController</code>.</p>
  
  <p>Funnily enough, if I run the following in the Rspec (not in a controller), the <code>UserSession.find</code> works properly and <code>just_created_session</code> is not nil.</p>
  
  <pre><code>UserSession.create(current_user)
  just_created_session = UserSession.find
  </code></pre>
  
  <p>So the problem is specific to <code>UserSession.find</code> being called in a controller.</p>
  
  <p>Any help is appreciated.</p>
  
  <p>Environment</p>
  
  <pre><code>Ruby: 1.9.3p392
  Rails: 3.2.12
  Authlogic: 3.2.0
  Factory Girl: 4.2.0
  Rspec: 2.13.0
  OS: Windows 7
  </code></pre>
  
  <p>Update:  I looked at <code>UserSession.create</code>, and all it does is this:</p>
  
  <pre><code>def create(*args, &amp;block)
      session = new(*args)
      session.save(&amp;block)
      session
  end
  </code></pre>
  
  <p>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 <code>User.find</code> to find anything.</p>