Q:

Rspec - How to include a "before_action"?

I have a before_action in my application_controller that uses a token to authorize the API access.
before_action :authenticate
def authenticate
    User.find_by_auth_token(params[:token]) || render_unauthorized
end

How can I put it in my tests?
teams_controller_spec:
let(:valid_attributes) { { :name => 'Test', :year => 2014 } }

describe "GET index" do
    it "assigns all teams as @teams" do
        team = Team.create! valid_attributes
        get :index, {}, valid_session
        assigns(:teams).should eq([team])
    end
end

When I run the tests I got the error:
expected: #<Team id: 1, name: "Test", year: 2014, created_at: "2014-08-13 12:07:49", updated_at: "2014-08-13 12:07:49"> 
got: nil

If I remove the authenticate the test pass.

A:

You are authenticating request using params[:token]
One way to solve your problem is create valid user with auth token using FactoryGirl before running test 
example:
 before(:each) do
    @user = FactoryGirl.create(:user)
  end 

and when writing test , pass user token
describe "GET index" do
    it "assigns all teams as @teams" do
        team = Team.create! valid_attributes
        get :index, {token: @user.auth_token}, valid_session
        assigns(:teams).should eq([team])
    end
end

