开发者

Rspec Rails - Mock to be current_user to test the controller

开发者 https://www.devze.com 2023-02-13 21:54 出处:网络
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.

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 :

开发者_如何学Python
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 ?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消