Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@KATSUYA_YAMAGUCHI

rspecでDeviseのログイン機能を使用する方法

More than 3 years have passed since last update.

概要

Webサービスのログイン処理にDevise使用しています。その際、ログイン後の処理をrspecでテストするためにどうすればよいか分からなかったため、調査した結果を記載します。

手順

参考にしたサイト

① 事前準備をする。

spec/rails_helper.rb
・・・
include Warden::Test::Helpers
・・・
Rspec.configure do |config|
  ・・・
  config.include Warden::Test::Helpers
  ・・・
end

② FactoryGirlでテスト用のモデルを作成する。

spec/factories/user.rb
FactoryGirl.define do
  factory :user do
    email "admin@gmail.com"
    password "1234567"
  end
end

login_asを使用して、テストを行う。

spec/requests/management_spec.rb
require 'rails_helper'

describe "management" do
  let(:user){ create(:user) }
  context "Get admin_path" do
    it "Response is 200." do
      # ログインする。
      login_as(user)

      get admin_path
      expect(response).to have_http_status(200) 
    end
  end
end

3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?