【发布时间】:2015-11-18 17:53:59
【问题描述】:
我正在尝试为我编写的 API 编写集成测试。 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 中调用的 authenticate 方法是:
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 的正确方法是什么?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 integration-testing