LoginSignup
6
10

More than 5 years have passed since last update.

Rspecでdeviseでログイン状態を作る

Posted at

FactoryGirlでUserモデル(devise)のファクトリを追加

spec/factories/user.rb
FactoryGirl.define do
    factory :user do
        username "Johney"
        sequence(:email) { |n| "johney#{n}@test.com"}
        password "password"
    password_confirmation "password"
    end
end

ログイン状態を生成

コントローラースペック

spec/controllers/xxxxx.controller_spec.rb
before do
  # ユーザー作成
  @user = create(:user)

  # サインイン
  sign_in @user
end

after do
  # サインアウト
  sign_out @user
end

フィーチャスペック

spec/features/xxxxx.spec.rb
require 'rails_helper'

feature 'feature' do

    scenario "scenario" do

        user = create(:user)

        visit root_path

        click_on 'Log in'
        find('#login_view').fill_in 'Email', with: user.email
        find('#login_view').fill_in 'Password', with: user.password
        click_button 'Log in'

        visit root_path

    end

end

参考

6
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
10