2
我正在嘗試爲我寫的API編寫集成測試。 api在訪問之前需要授權。Rails 3.2.13:授權失敗的API集成測試
我使用FactoryGirl,用下面的工廠爲我的用戶:
factory :user do
name "John Doe"
sequence(:email) { |n| "person-#{n}@testSuite.com" }
password "password"
password_confirmation { |u| u.password }
api_key "1234567890"
end
被稱爲在我的控制器的before_filter的身份驗證方法是:
def authenticate
authenticate_or_request_with_http_token do |token, options|
@current_user = User.where(api_key: token).first
end
end
而且我的測試是
test 'User Index' do
@user = FactoryGirl.create(:user)
get '/api/v1/users', authorization: "Token token=\"#{@user.api_key}\""
assert_response :success
end
該測試連續失敗,並返回401響應。
設置授權令牌的正確方法是什麼,以便我可以真正連接到API?
這工作。非常感謝你! – stats