Spyros
Spyros

Reputation: 48596

Rspec Rails - Mock to be current_user to test the controller

i've started writing tests for my controllers. For once of them, i need to be logged in, in order to check whether the page is rendered etc.

Since i'm using Devise that has a current_user helper over a User model, how can i form my :

describe "when it GETS 'town'" do
  it "should render the template and be successful" do
    get :index
    response.should render_template(:index)
    response.should be_success
  end
end

so that i don't get the undefined "authenticate!" method (devise specific) error ?

Upvotes: 4

Views: 16667

Answers (1)

zetetic
zetetic

Reputation: 47548

There's a good writeup on this topic on the Devise wiki. Basically you need to add the Devise test helpers, then call sign_in to get a valid current user when your spec expects one.

Upvotes: 14

Related Questions