Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 540
Spyros Avatar asked Dec 10 '22 10:12

Spyros


1 Answers

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.

like image 197
zetetic Avatar answered Dec 30 '22 08:12

zetetic