0
私はdevise_auth_tokenを使用して、APIのユーザを認証しています。各テストを実行する前にユーザーを認証したいが、401エラーが発生する。正しいヘッダーでエンドポイントにpostmanを使用すると機能しますが、テスト中は機能しません。RSpec:devise_auth_tokenによるテスト前の認証
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'を使用できませんか? –