首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试控制器中的所有操作只允许经过身份验证的用户

测试控制器中的所有操作只允许经过身份验证的用户
EN

Stack Overflow用户
提问于 2015-12-09 20:31:47
回答 1查看 457关注 0票数 1

我的Rails 4应用程序中有一个小型REST API。基本上,它是一个有7个动作的控制器。只有经过身份验证的用户才能访问API,并且检查被添加为Devise中的before_filter。最好的测试方法是什么?

我已经对API进行了单元测试,如果需要身份验证,可以手动测试每个操作,但我显然不喜欢这样做。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-09 21:02:50

在RSpec中您可以使用使用共享示例

代码语言:javascript
复制
# spec/support/shared_examples/authentication.rb
RSpec.shared_examples "an authenticated action" do |action|
  it "requires authentication" do
    expect { action.call }.to raise_exception("uncaught throw :warden")
  end
end
代码语言:javascript
复制
require 'rails_helper'
require 'support/shared_examples/authentication'

RSpec.describe PetsController do
  describe "GET #index" do
   it_behaves_like "an authenticated action", { get :index }
  end
  describe "PATCH #update" do
   it_behaves_like "an authenticated action", { patch :update, { pet: { name: 'Moon Moon' }} }
  end
  describe "DELETE #destroy" do
   it_behaves_like "an authenticated action", { delete :destroy, id: 1 }
  end
end
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34188420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档