2011-09-16 85 views
4

如何使用rspec2測試登錄到使用http basic認證的rails 3.1應用程序& capybara?rails rspec http基本認證測試

我正在使用這個;

describe "GET 'index'" do 
     it "should be successful" do 
     request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password") 
     get 'index' 
     response.should be_success 
     end 
    end 

但它給了我這個錯誤;

NoMethodError: 
      undefined method `env' for nil:NilClass 
+0

看到http://stackoverflow.com/questions/4329985/http-basic-auth-for-capybara – zetetic

回答

-1
@request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password") 

這應該是:

request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password") 

request不是一個實例變量。

+0

我得到同樣的錯誤 –

+0

請張貼整個文件。 – Gazler

+0

我在這裏使用的解決方案http://stackoverflow.com/questions/3768718/rails-rspec-make-tests-to-pass-with-http-basic-authentication –