Rspec Rails - 模拟为current_user来测试控制器

Spy*_*ros 4 rspec ruby-on-rails

我开始为我的控制器编写测试.对于其中一次,我需要登录,以检查页面是否呈现等.

由于我使用的是具有current_user帮助器的Devise而不是User模型,我如何形成我的:

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
Run Code Online (Sandbox Code Playgroud)

所以我没有得到未定义的"认证!" 方法(设计具体)错误?

zet*_*tic 14

Devise wiki上有关于这个主题的很好的写作.基本上你需要添加Devise测试助手,然后sign_in在你的规范期望时调用以获得有效的当前用户.