2
我在Rails 3應用程序中有一個非常基本的HTTP摘要身份驗證設置。它主要遵循了Rails Controller Guide發現的例子:HTTP摘要+ Rspec
我的ApplicationController擁有的before_filter:
def digest_authenticate
success = authenticate_or_request_with_http_digest("Application") do |username|
APP_CONFIG["admin"]
end
end
這一切的偉大工程。頁面受到保護,因爲它們應該是。
我現在試圖在RSpec中測試它,並且失敗了。
我跟着this SO accepted answer,並將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"]
中存儲的內容相匹配嗎? - 如果我從
digest_authenticate
的before_filter打印出success
的值,即使我將正確的參數傳遞給我的rspec幫助器,也總是打印出401。
任何想法如何有效地測試HTTP摘要認證?
謝謝!
在控制器測試中,您應該使用'get:index'而不是'visit root_path'。訪問方法是請求spec/capybara工具而不是控制器測試工具。 – nmott
啊,完美!請將此作爲答案,以便我可以將其標記爲正確。 – djibouti33