2011-02-27 117 views
4

我已经开始为我的控制器编写测试。对于其中一次,我需要登录,以检查页面是否呈现等。Rspec Rails - 模拟成为current_user来测试控制器

因为我使用的Devise在用户模型上有一个current_user helper,我该如何形成我的:

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 

这样我就不会得到未定义的“验证!”方法(设计特定)错误?

回答

14

Devise wiki上有关于此主题的很好的写法。基本上你需要添加设计测试助手,然后调用sign_in来获得一个有效的当前用户,当你的规范期望一个。