お世話になっております。validationについての質問です。
前提・実現したいこと
railsのバリデーションでemailの大文字小文字の区別をするためにcase_sensitive:trueにしたい。
発生している問題・エラーメッセージ
Userモデルに明示的にcase_sensitive: trueと書いたにも関わらず、テストがパスしない。
User DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from block (2 levels) in <main> at /usr/src/app/spec/models/user_spec.rb:6) is valid with name, email and password emailの小文字と大文字を識別できるか DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from block (3 levels) in <main> at /usr/src/app/spec/models/user_spec.rb:11) DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from block (3 levels) in <main> at /usr/src/app/spec/models/user_spec.rb:13) 全く同じemailアドレスの時 DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from block (3 levels) in <main> at /usr/src/app/spec/models/user_spec.rb:17) DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from block (3 levels) in <main> at /usr/src/app/spec/models/user_spec.rb:19) 大文字小文字の区別がある時 (FAILED - 1) Failures: 1) User emailの小文字と大文字を識別できるか 大文字小文字の区別がある時 Failure/Error: expect(user3).to be_valid expected #<User id: nil, provider: "email", uid: "", name: "test2", image: nil, email: "example@example.com", allow_password_change: false, created_at: nil, updated_at: nil> to be valid, but got errors: Email has already been taken, Email has already been taken # ./spec/models/user_spec.rb:19:in `block (3 levels) in <main>' # /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load' # /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load' # /usr/local/bundle/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call' # -e:1:in `<main>' Finished in 0.28343 seconds (files took 0.29851 seconds to load) 3 examples, 1 failure Failed examples: rspec ./spec/models/user_spec.rb:16 # User emailの小文字と大文字を識別できるか 大文字小文字の区別がある時
該当のソースコード
#####user_spec.rb
ruby
1require "rails_helper" 2 3RSpec.describe User, type: :model do 4 it "is valid with name, email and password" do 5 user = FactoryBot.build(:user) 6 expect(user).to be_valid 7 end 8 9 context "emailの小文字と大文字を識別できるか" do 10 it "全く同じemailアドレスの時" do 11 user = FactoryBot.create(:user) 12 user2 = FactoryBot.build(:user, name: "test2") 13 expect(user2).not_to be_valid 14 end 15 16 it "大文字小文字の区別がある時" do 17 user = FactoryBot.create(:user) 18 user2 = FactoryBot.build(:user, name: "test2", email: "Example@example.com") 19 expect(user2).to be_valid 20 end 21 end 22end 23
#####user.rb
ruby
1# frozen_string_literal: true 2 3class User < ActiveRecord::Base 4 # Include default devise modules. Others available are: 5 # :confirmable, :lockable, :timeoutable and :omniauthable 6 7 devise :database_authenticatable, :registerable, 8 :recoverable, :rememberable, :trackable, :validatable 9 include DeviseTokenAuth::Concerns::User 10 validates :email, uniqueness: { case_sensitive: true } 11end 12
#####spec_helper.rb
ruby
1require 'database_cleaner' 2 3RSpec.configure do |config| 4 5 # databaseを空にする 6 config.before(:suite) do 7 DatabaseCleaner.strategy = :truncation 8 end 9 config.before(:each) do 10 DatabaseCleaner.start 11 end 12 config.after(:each) do 13 DatabaseCleaner.clean 14 end 15 16 config.expect_with :rspec do |expectations| 17 expectations.include_chain_clauses_in_custom_matcher_descriptions = true 18 end 19 20 config.mock_with :rspec do |mocks| 21 mocks.verify_partial_doubles = true 22 end 23 24 config.shared_context_metadata_behavior = :apply_to_host_groups 25end
#####spec/factories/users.rb
ruby
1FactoryBot.define do 2 factory :user do 3 name {"test"} 4 email {"example@example.com"} 5 password {"password"} 6 password_confirmation {"password"} 7 end 8end 9
試したこと
明示的にcase_sensitive: falseにしてみましたが、rspecの結果が全く同じです。
補足情報(FW/ツールのバージョンなど)
devise 4.7.1
devise_token_auth 1.1.3
factory_bot_rails 5.1.1
ruby 2.6.3
rails 6.0.2.2
rspec-rails 4.0.0
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。