【发布时间】:2015-03-10 11:28:58
【问题描述】:
我正在使用带有 REST API 的 Rails 编写应用程序。除非用户已获得授权,否则大多数控制器都无法访问。这是通过在控制器中的before_action 挂钩中插入一个检查用户权限的方法来完成的。我想测试未经授权的用户无法访问 API 的某些部分。目前我是这样做的:
需要'rails_helper'
RSpec.describe RoomsController, type: :controller do
...
describe "while unauthenticated" do
before do
logout
end
def expect_unauth
expect(response).to have_http_status(:unauthorized)
end
it "GET #index returns http unauthorized" do get :index; expect_unauth end
it "GET #show returns http unauthorized" do get :show, {id: 1}; expect_unauth end
it "DELETE #destroy returns http unauthorized" do delete :destroy, {id: 1}; expect_unauth end
it "POST #create returns http unauthorized" do post :create, {id: 1}; expect_unauth end
it "PUT #update returns http unauthorized" do put :update, {id: 1}; expect_unauth end
end
它可以工作,但对于每个控制器来说几乎都是一样的。如何在不将这段代码复制粘贴到每个控制器中的情况下进行这样的测试?我是否应该对其进行测试,或者因为它非常简单,我应该假设它可以工作并为特定于控制器功能的特定功能编写测试?
另外,它甚至属于控制器规格吗?也许它应该是一个请求规范?
【问题讨论】:
-
Rspec 有一个称为共享示例的功能。也许这就是你要找的东西:relishapp.com/rspec/rspec-core/docs/example-groups/…
-
如果我有时间我会为此创建自定义匹配器。在此处阅读如何执行此操作:relishapp.com/rspec/rspec-expectations/v/2-4/docs/…。理想情况下,我希望它阅读
it { should require_login }以了解控制器上定义的所有操作,或者如果控制器部分公开,则阅读it { should require_login.for(:edit)。这是一项艰巨的任务(但绝对可行),所以玩得开心。 :)
标签: ruby-on-rails ruby-on-rails-3 unit-testing ruby-on-rails-4 rspec