1
0

More than 1 year has passed since last update.

【RSpecエラー】validate_uniqueness_ofを使うとテストが通らないとき

Last updated at Posted at 2021-10-28

環境

Ruby 3.0.2
Rails 6.1.4.1

状況

バリデーションのテスト実行時に下記のエラー
デフォルトでは大文字と小文字を区別している

model.rb
validates :email, uniqueness: true
spec.rb
it { is_expected.to validate_uniqueness_of(:email) }
Expected Employee to validate that :email is case-sensitively unique,
       but this could not be proved.
         After taking the given Employee, setting its :email to ‹"dummy
         value"›, and saving it as the existing record, then making a new
         Employee and setting its :email to a different value, ‹"DUMMY VALUE"›,
         the matcher expected the new Employee to be valid, but it was invalid
         instead, producing these validation errors:

解決法

大文字小文字の区別なくテストするignoring_case_sensitivityメソッドを追加

spec.rb
it { is_expected.to validate_uniqueness_of(:email).ignoring_case_sensitivity }

参考

1
0
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
1
0