Nic*_*fal 5 ruby controller rspec ruby-on-rails basic-authentication
我正在开发 MVP(最小可行产品)。为了提供一种更简单的方法来保护管理页面,我只是将 http_basic_authenticate_with 添加到我的 AdminController 中。
问题是,当我想测试我的 AdminController 时,我因未登录而收到“未经授权”(401)。
在这种情况下,测试身份验证是无关紧要的——它只是暂时的,一旦我进入下一个冲刺,它就会被删除——所以我试图在 RSpec 中跳过它。
问题是我尝试了很多方法,但似乎都不起作用。
例如,我尝试修改http_basic_authenticate_with以避免身份验证。像这样:
require 'spec_helper'
module ActionController
module HttpAuthentication
module Basic
def http_basic_authenticate_with(*args)
end
end
end
end
describe Admin::SubscribersController do
describe "GET 'index'" do
it "should be OK" do
get 'index'
response.should be_successful
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,对于这个简单的测试它仍然返回“false”。
顺便说一句,为了简化这个测试,我的 AdminController 上只有一个空索引操作和一个空视图 (index.html.erb)。
终于我成功了。
文档中所述的内容对我不起作用:
get 'index', nil, 'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials("admin", "password")
Run Code Online (Sandbox Code Playgroud)
所以我尝试了一种“旧”方法来做到这一点,即设置request.env['HTTP_AUTHORIZATION']:
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials("admin","password")
Run Code Online (Sandbox Code Playgroud)
其他解决方案都不起作用,所以我将继续使用这个解决方案。
谢谢。
| 归档时间: |
|
| 查看次数: |
4769 次 |
| 最近记录: |