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

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

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby on Rails

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

Q&A

1回答

2277閲覧

フリマアプリのモデル単体テストを実装しているが、エラーが3ヶ所出るのを解決したい

hiroyagima

総合スコア0

Ruby on Rails

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

0グッド

0クリップ

投稿2021/11/23 02:39

編集2021/11/23 05:23

前提・実現したいこと

フリマアプリのモデル単体テストを実装中ですが、(FAILED - )として3ヶ所エラーが出ます。間違いである記述を見つけることができません。

発生している問題・エラーメッセージ

User ユーザー新規登録 新規登録できる場合 nickname、email、password、password_confirmation、first_name、last_name、first_name_kana、last_name_kana、birthdayが存在すれば登録できる 新規登録ができない時 nicknameが空では登録できない first_nameが空では登録できない last_nameが空では登録できない first_name_kanaが空では登録できない last_name_kanaが空では登録できない first_name_kanaのフリガナは全角(カタカナ)でなければ登録できない (FAILED - 1) last_name_kanaのフリガナは全角(カタカナ)でなければ登録できない (FAILED - 2) birthdayが空では登録できない emailが空では登録できない 重複したemailが存在する場合登録できない passwordが空では登録できない password_confirmationが空では登録できない passwordとpassword_confirmationが不一致では登録できない passwordが5文字以下では登録できない passwordが英数混合でないと登録できない (FAILED - 3) Failures: 1) User ユーザー新規登録 新規登録ができない時 first_name_kanaのフリガナは全角(カタカナ)でなければ登録できない Failure/Error: expect(@user.errors.full_messages).to include("fitst name kana がカタカナで入力されていない") expected ["First name kana is invalid"] to include "fitst name kana がカタカナで入力されていない" # ./spec/models/user_spec.rb:43:in `block (4 levels) in <top (required)>' 2) User ユーザー新規登録 新規登録ができない時 last_name_kanaのフリガナは全角(カタカナ)でなければ登録できない Failure/Error: expect(@user.errors.full_messages).to include("Last name kana can't be blank") expected ["Last name kana is invalid"] to include "Last name kana can't be blank" # ./spec/models/user_spec.rb:48:in `block (4 levels) in <top (required)>' 3) User ユーザー新規登録 新規登録ができない時 passwordが英数混合でないと登録できない Failure/Error: expect(@user.errors.full_messages).to include("Password No alphanumeric mixture") expected [] to include "Password No alphanumeric mixture" # ./spec/models/user_spec.rb:92:in `block (4 levels) in <top (required)>' Finished in 0.11392 seconds (files took 1.31 seconds to load) 16 examples, 3 failures Failed examples: rspec ./spec/models/user_spec.rb:40 # User ユーザー新規登録 新規登録ができない時 first_name_kanaのフリガナは全角(カタカナ)でなければ登録できない rspec ./spec/models/user_spec.rb:45 # User ユーザー新規登録 新規登録ができない時 last_name_kanaのフリガナは全角(カタカナ)でなければ登録できない rspec ./spec/models/user_spec.rb:88 # User ユーザー新規登録 新規登録ができない時 passwordが英数混合でないと登録できない

該当のソースコード

ruby on rails
spec/factories/users.rb FactoryBot.define do factory :user do nickname {Faker::Name.initials(number: 2)} first_name {'山田'} last_name {'太郎'} first_name_kana {'ヤマダ'} last_name_kana {'タロウ'} birthday {'1930-01-01'} email {Faker::Internet.free_email} password {Faker::Internet.password(min_length: 6)} password_confirmation {password} end end
spec/models/user_spec.rb require 'rails_helper' RSpec.describe User, type: :model do before do @user = FactoryBot.build(:user) end describe 'ユーザー新規登録' do context '新規登録できる場合' do it 'nickname、email、password、password_confirmation、first_name、last_name、first_name_kana、last_name_kana、birthdayが存在すれば登録できる' do expect(@user).to be_valid end end context '新規登録ができない時' do it 'nicknameが空では登録できない' do @user.nickname = '' @user.valid? expect(@user.errors.full_messages).to include("Nickname can't be blank") end it 'first_nameが空では登録できない' do @user.first_name = '' @user.valid? expect(@user.errors.full_messages).to include("First name can't be blank") end it 'last_nameが空では登録できない' do @user.last_name = '' @user.valid? expect(@user.errors.full_messages).to include("Last name can't be blank") end it 'first_name_kanaが空では登録できない' do @user.first_name_kana = '' @user.valid? expect(@user.errors.full_messages).to include("First name kana can't be blank") end it 'last_name_kanaが空では登録できない' do @user.last_name_kana = '' @user.valid? expect(@user.errors.full_messages).to include("Last name kana can't be blank") end it "first_name_kanaのフリガナは全角(カタカナ)でなければ登録できない" do @user.first_name_kana = "aiueo" @user.valid? expect(@user.errors.full_messages).to include("fitst name kana がカタカナで入力されていない") end it "last_name_kanaのフリガナは全角(カタカナ)でなければ登録できない" do @user.last_name_kana = "aiueo" @user.valid? expect(@user.errors.full_messages).to include("Last name kana がカタカナで入力されていない") end it 'birthdayが空では登録できない' do @user.birthday = '' @user.valid? expect(@user.errors.full_messages).to include("Birthday can't be blank") end it 'emailが空では登録できない' do @user.email = '' @user.valid? expect(@user.errors.full_messages).to include("Email can't be blank") end it '重複したemailが存在する場合登録できない' do @user.save another_user = FactoryBot.build(:user) another_user.email = @user.email another_user.valid? expect(another_user.errors.full_messages).to include("Email has already been taken") end it 'passwordが空では登録できない' do @user.password = '' @user.valid? expect(@user.errors.full_messages).to include("Password can't be blank") end it "password_confirmationが空では登録できない" do @user.password_confirmation = '' @user.valid? expect(@user.errors.full_messages).to include("Password confirmation doesn't match Password") end it 'passwordとpassword_confirmationが不一致では登録できない' do @user.password_confirmation = '' @user.valid? expect(@user.errors.full_messages).to include("Password confirmation doesn't match Password") end it 'passwordが5文字以下では登録できない' do @user.password = 'abc12' @user.password_confirmation = 'abc12' @user.valid? expect(@user.errors.full_messages).to include("Password is too short (minimum is 6 characters)") end it 'passwordが英数混合でないと登録できない' do @user.password = '12345678' @user.password_confirmation = '12345678' @user.valid? expect(@user.errors.full_messages).to include("Password No alphanumeric mixture") end end end end

試したこと

include("Last_name_kana がカタカナで入力されていない")
↓のように、includeの続き、_の記述が不要な文を消したが、エラー分が出てくる。(いくつかの場所は、この方法で解決をした。)
include("Last name kana がカタカナで入力されていない")

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

1T2R3M4

2021/11/23 02:45

調べたこと、試したことを質問に追記していただけませんか。
guest

回答1

0

間違いである記述を見つけることができません。

上2つはエラーメッセージが想定していた形と違っただけです。「エラーメッセージの文言自体を検証したい」という意志があるのなら別ですが、そうでないなら「エラーになるかならないか」だけ検証したほうが適当かなと考えます。

一方、「passwordが英数混合でないと登録できない」は検証の問題ではなく、本当にその機能が正しく実装されていないように見受けられます(Userの実装が提示されていないので、なんとも言えません)。

投稿2021/11/23 02:50

maisumakun

総合スコア145208

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問