我有一个非常基本的HTTP摘要认证设置在我的Rails 3应用程序上。它主要遵循Rails控制器指南中的示例
我的ApplicationController有一个before_filter:
def digest_authenticate
success = authenticate_or_request_with_http_digest("Application") do |username|
APP_CONFIG["admin"]
end
end一切都很好。页面按应有的方式受到保护。
我现在正试图在RSpec中测试这一点,结果失败了。
我遵循这个被接受的答案并将authenticate_with_http_digest方法放在一个支持文件中。这是我的实际测试:
describe DashboardController do
describe "GET 'index'" do
it "returns http success" do
authenticate_with_http_digest(foo, bar, baz)
visit root_path
response.should be_success
response.code.should == '200'
end
end
end几个问题:
authenticate_with_http_digest,每次测试都是通过的。authenticate_with_http_digest的论点是假的,似乎并不重要。这些不应该与我在APP_CONFIG["admin"]中存储的东西相匹配吗?success digest_authenticate before_filter中打印出before_filter的值,它总是打印401,即使我确实将正确的参数传递给了rspec助手。如何有效地测试HTTP摘要身份验证?
谢谢!
发布于 2012-12-20 00:16:32
对于控制器测试,您应该使用get :index调用而不是visit root_path调用。这将适用于正在进行控制器测试的HTTP谓词和Rails操作的任何有效组合。
visit方法是Capybara的一部分,只适用于请求规范。
https://stackoverflow.com/questions/13946463
复制相似问题