【发布时间】:2017-05-13 05:49:45
【问题描述】:
我正在使用devise_auth_token 对 API 的用户进行身份验证。我想在每次测试运行之前对用户进行身份验证,但不断收到 401 错误。当我使用邮递员到具有正确标头的端点时,它可以工作,但在测试期间无法工作。
before(:each) do
@user = FactoryGirl.create(:user)
end
def get_auth
headers = @user.create_new_auth_token
auth = Hash.new
auth["client"] = headers["client"]
auth["access-token"] = headers["access-token"]
auth["uid"] = headers["uid"]
auth["expiry"] = headers["expiry"]
return auth
end
it "auth user should return success" do
get 'get_tasks_for_user', params: {uid: @user.uid}, headers: get_auth
expect(response).to have_http_status(200)
end
RSpec
TasksController auth user should return success
Failure/Error: expect(response).to have_http_status 200
expected the response to have status code 200 but it was 401
【问题讨论】:
-
你不能在
before(:each)块中使用sign_in @user吗?
标签: ruby-on-rails ruby testing rspec devise