質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

3997閲覧

Email has already been takenと出てテストがパスしない

msickpaler

総合スコア14

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/04/24 08:21

編集2020/04/29 02:08

お世話になっております。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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

エラーの"Email has already been taken"から、

「DBがロールバックされずにデータが残っているのではないか」

との指摘を受け、下記の記事を参考にテスト環境のDBを初期化するとうまくいきました。
リンク内容

投稿2020/04/28 11:16

msickpaler

総合スコア14

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問