【发布时间】:2017-11-22 10:12:38
【问题描述】:
我想知道是否有比我目前更好的方法来编写控制器请求规范。我正在使用设计 gem 进行身份验证。这就是我测试管理控制器的方式:
describe "#index" do
context "when not logged in" do
it "redirects to root page" do
get admin_index_path
expect(response).to redirect_to root_path
end
end
context "when logged in as an user" do
before { sign_in user }
it "redirects to root page" do
get admin_index_path
expect(response).to redirect_to root_path
end
end
context "when logged in as an admin" do
before { sign_in admin }
it "opens the page" do
get admin_index_path
expect(response).to be_success
end
end
end
如您所见,有一些“样板”代码在我的许多控制器上重复出现。对于需要用户登录的控制器,我必须为每个控制器操作编写“未登录”规范。你怎么做到这一点?有没有办法缩短/共享规范之间的代码?唯一改变的是路径。
【问题讨论】:
-
用
include_examples替换it_behaves_like
标签: ruby-on-rails rspec devise