2012-12-19 58 views
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 

的幾個問題:

  1. 的測試都通過每一次,我是否不叫authenticate_with_http_digest
  2. 我傳遞給authenticate_with_http_digest的參數是假的,並似乎並不重要。這些不應該與我在APP_CONFIG["admin"]中存儲的內容相匹配嗎?
  3. 如果我從digest_authenticate的before_filter打印出success的值,即使我將正確的參數傳遞給我的rspec幫助器,也總是打印出401。

任何想法如何有效地測試HTTP摘要認證?

謝謝!

+1

在控制器測試中,您應該使用'get:index'而不是'visit root_path'。訪問方法是請求spec/capybara工具而不是控制器測試工具。 – nmott

+0

啊,完美!請將此作爲答案,以便我可以將其標記爲正確。 – djibouti33

回答

2

對於控制器測試,您應該使用get :index呼叫而不是visit root_path呼叫。這將適用於控制器測試的HTTP動詞和Rails動作的任何有效組合。

visit方法是水豚的一部分,只能在請求規格中使用。