我试图限制我的应用程序中的一些控制器在任何操作之前需要登录。我知道如何实现它,但不知道如何在 rspec 中为它编写好的测试。
例如,如果我想限制用户控制器的每个操作都需要登录,我可以进行如下测试:
描述“授权”做
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "in the Users controller" do
describe "visiting the index page" do
before { visit users_path }
it { should have_selector('title', text: 'Log In') }
end
describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_selector('title', text: 'Log In') }
end
describe "submitting to the update action" do
before { put user_path(user) }
specify { response.should redirect_to(login_path) }
end
describe "submitting a DELETE request to the Users#destroy action" do
before { delete user_path(user) }
specify { response.should redirect_to(root_path) }
end
....etc.....
end
end
我是否需要为要测试的每个控制器指定所有 7 条宁静路由?似乎效率很低。有没有办法说“在访问任何用户路由响应之前应该重定向到 login_path”?