title: How can I access Sorcery in my RSpec tests?
body: <p>Sorcery authentication gem: <a href="https://github.com/NoamB/sorcery" rel="nofollow noreferrer">https://github.com/NoamB/sorcery</a></p>
  
  <p>Sorcery's creator provides an example Rails app with Sorcery test helpers included in its Test::Unit functional tests: <a href="https://github.com/NoamB/sorcery-example-app/blob/master/test/functional/users_controller_test.rb" rel="nofollow noreferrer">https://github.com/NoamB/sorcery-example-app/blob/master/test/functional/users_controller_test.rb</a></p>
  
  <pre><code># Test::Unit functional test example
  require 'test_helper'
  
  class UsersControllerTest &lt; ActionController::TestCase
    setup do
      @user = users(:noam)
    end
  
    test "should show user" do
      login_user
      get :show, :id =&gt; @user.to_param
      assert_response :success
    end
  </code></pre>
  
  <p>But I can't figure out how to get <code>login_user</code> to work in my RSpec controller specs.</p>
  
  <pre><code>/gems/sorcery-0.7.5/lib/sorcery/test_helpers/rails.rb:7:in `login_user': 
  undefined method `auto_login' for nil:NilClass (NoMethodError)
  </code></pre>
  
  <p>Here's the relevant code in the Sorcery gem regarding the above error:
  <a href="https://github.com/NoamB/sorcery/blob/master/lib/sorcery/test_helpers/rails.rb" rel="nofollow noreferrer">https://github.com/NoamB/sorcery/blob/master/lib/sorcery/test_helpers/rails.rb</a></p>
  
  <pre><code>module Sorcery
    module TestHelpers
      module Rails
        # logins a user and calls all callbacks
        def login_user(user = nil)
          user ||= @user
          @controller.send(:auto_login,user)
          @controller.send(:after_login!,user,[user.send(user.sorcery_config.username_attribute_names.first),'secret'])
        end
  
        def logout_user
          @controller.send(:logout)
        end
      end
    end
  end
  </code></pre>
  
  <h1>UPDATE:</h1>
  
  <p>As per Sorcery's documentation "<a href="https://github.com/NoamB/sorcery/wiki/Testing-rails-3" rel="nofollow noreferrer">Testing in Rails 3</a>", I have indeed added <code>include Sorcery::TestHelpers::Rails</code> to my <code>spec_helper.rb</code>.</p>
  
  <p>The Sorcery test helper <code>login_user</code> acts on <code>@controller</code>, but I'm getting the error because <code>@controller</code> is <code>nil</code> in my controller spec. Here's my spec:</p>
  
  <pre><code>#spec/controllers/forums_controller_spec.rb
  require 'spec_helper'
  
  describe ForumsController do
    render_views
  
    describe 'GET new' do
      describe 'when guest' do
        it 'should deny and redirect' do
          get :new
          response.should redirect_to(root_path)
        end
      end
  
      describe 'when admin' do
        p @controller #=&gt; nil
        @user = User.create!(username: "Test", password: "secret", email: "test@test.com")
        login_user # &lt;--------------- where the error occurs
        it 'should resolve' do
          get :new
          response.should render_template(:new)
        end
      end
    end
  end
  </code></pre>