2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SystemSpecでログインできない時のお話

Last updated at Posted at 2019-12-14

新たにSystem Specを導入しようとした時のお話です。

事象

ログイン処理を実装した時にログインができませんでした。

条件

・ローカルの画面上ではログインできる
・Factoryのデータ作成もできている
・他のSpecにて、Factoryを使用したデータではログインできる = Factoryで作成するデータ構造は正しい
・Feature Specでは成功した = System Specの記述もおかしくなさそう

結論

Database Cleanerを導入していてFactoryで作成したデータが消えたためログインできなかったようです。

解決方法

Database Cleanerの記述自体を消した時に他のSpecの挙動がおかしくなったから次の書き方で対応しました。
use_truncation: false を適用させてDatabase Cleanerを無効
参照
https://qiita.com/takeyuweb/items/e7261e9274b3b31d933c

XXXX_spec.rb
RSpec.describe 'XXXX', type: :system do
  # ここにはDatabase Cleanerを適用させない
  context "Login", use_truncation: false do
    it 'is login test' do
      expect(page).to have_content 'ログインしました'
    end    
  end
end
rails_helper.rb
RSpec.configure do |config|
.
.
  config.use_transactional_fixtures = true
.
.
end

Database Cleanerについて

テスト毎にデータベースを空(truncate)にする機能。

Database Cleanerの設定記述を消してSpec実行してみると
テストテーブルにデータが残っていることを確認できます。

伊藤さんのブログ(Database Cleaner)より抜粋します。

Rails 5.1ではDatabase Cleanerはもう必要ありません。

基本的にはDatabase Cleaner自体導入しなくても良さそうです。
途中からSystem Specを導入しようとしたら同じような事象が発生するかもしれませんね。

今まで特にDatabase Cleanerを意識したことがないので良い経験になりました。

他の皆さんはこのような事象に遭遇したことありますか?
もし、経験された方がいましたらどのような対応されたか教えていただけると嬉しいです。

追記

System Specでは データベースが自動的にロールバックされる みたいです。
(データベースの自動ロールバック)
https://techracho.bpsinc.jp/hachi8833/2018_01_25/51101

だからFeatureSpecでは同じ記述でもログインできました。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?