title: Adding helper module to Rspec
body: <p>I would like to add a module that includes a method to help me log in as a user. Here it is:</p>
  
  <pre><code>module TestHelper
    require 'spec_helper'
  
    ALL_USERS = ["administrator", "instructor", "regular_user"]
  
    def self.login_as(user_type)
      user = User.find_by(global_role_id: GlobalRole.send(user_type))
      @request.env["devise.mapping"] = Devise.mappings[:user]
      sign_in user
    end
  end
  </code></pre>
  
  <p>The spec that's calling it is</p>
  
  <pre><code>require 'spec_helper'
  
  RSpec.describe QuestionsController, :type =&gt; :controller do
    include Devise::TestHelpers
    include TestHelper
  
    describe "a test" do
      it "works!" do
        TestHelper.login_as("administrator")
      end
    end
  end
  </code></pre>
  
  <p>And here is the spec_helper</p>
  
  <pre><code>RSpec.configure do |config|
    config.include TestHelper, :type =&gt; :controller
  </code></pre>
  
  <p>The error I get is: <code>undefined method 'env' for nil:NilClass</code> It looks like my module doesn't have access to <code>@request</code>.</p>
  
  <p>My question is: how do I access and <code>@request</code> in the external Module?</p>