ruby-on-rails – Rails中的Rspec授权
发布时间:2020-12-16 21:12:20 所属栏目:百科 来源:网络整理
导读:我正在使用rails 4开发rails API,我想测试它.为此,我使用的是Rspec框架. 我的API有一个身份验证方法,如下所示: class UsersController ApplicationController # Token authentication before_action :set_user,except: [:index,:create] before_action :aut
我正在使用rails 4开发rails API,我想测试它.为此,我使用的是Rspec框架.
我的API有一个身份验证方法,如下所示: class UsersController < ApplicationController # Token authentication before_action :set_user,except: [:index,:create] before_action :authenticate,:create] before_action :authenticate_admin,only: [:index,:create] ... end 然后我有我的UsersController Rspec: describe UsersController do render_views describe "GET /users" do it "gets all users" do @request.env["AUTHORIZATION"] = "Token token=xxxxxxxxx" @request.env["HTTP_ACCEPT"] = "application/json" @request.env["CONTENT_TYPE"] = "application/json" get :index response.status.should be(200) end end end 当我执行测试时,它总是给我一样的: UsersController GET /users getting all users Failure/Error: response.status.should be(200) expected #<Fixnum:401> => 200 got #<Fixnum:803> => 401 ... 我使用authenticate_or_request_with_http_token方法从标头中获取令牌. 如果有人可以帮我找到跳过before_action方法或正确设置Authentication头的方法,我将非常感激. 谢谢! 解决方法
我终于解决了这个问题.
错误是: request.env["AUTHORIZATION"] 一定是 request.env["HTTP_AUTHORIZATION"]. 除此之外,我从一个未在我的测试环境中创建的用户获取了一个错误的令牌.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- cocos引用iconv库在android编译,错误undefined reference l
- 关于IE11修改User-agent不再支持document.all等
- iphone – Core Data to SQL无法正常工作
- react-native – 在设备上运行React Native Android
- 修复SQLite-database disk image is malformed
- 正则表达式概述
- vb.net – 如何测试计算机是否安装了Excel(任何版本)?
- StAX-基于流的拉式XML解析
- Dojo 1.6 官方教程翻译:创建基于模板的小部件(Widget)
- PAT Basic 1024 科学计数法 (20 分) Advanced 1073 Scienti