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

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

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

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

RSpec

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

Q&A

1回答

613閲覧

rails rspec 単体テスト 正常に実行したい

kokosi1062

総合スコア39

Ruby on Rails 5

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

RSpec

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

0グッド

0クリップ

投稿2020/04/14 09:14

#前提・実現したいこと

railsで個人アプリを作成しています。そこで、初めて単体テストを行いました。
プログラミングスクールのカリキュラム通りに、記述したのですが、エラーが出てしまい
正常に実行が出来ないです。

#エラーメッセージ

rails

1User 2 #create 3 nicknameがない場合は登録できないこと (FAILED - 1) 4 5Failures: 6 7 1) User#create nicknameがない場合は登録できないこと 8 Failure/Error: expect(user.errors[:nickname]).to include("can't be blank") 9 expected ["を入力してください"] to include "can't be blank" 10 # ./spec/models/user_spec.rb:7:in `block (3 levels) in <top (required)>' 11 12Finished in 0.17061 seconds (files took 4.8 seconds to load) 131 example, 1 failure 14 15Failed examples: 16 17rspec ./spec/models/user_spec.rb:4 # User#create nicknameがない場合は登録できないこと

#ソースコード

rails

1require 'rails_helper' 2describe User do 3 describe '#create' do 4 it "nicknameがない場合は登録できないこと" do 5 user = User.new(nickname: "", email: "kkk@gmail.com", password: "00000000", password_confirmation: "00000000") 6 user.valid? 7 expect(user.errors[:nickname]).to include("can't be blank") 8 end 9 end 10end

jayml

1--- 2ja: 3 activerecord: 4 errors: 5 messages: 6 record_invalid: 'バリデーションに失敗しました: %{errors}' 7 restrict_dependent_destroy: 8 has_one: "%{record}が存在しているので削除できません" 9 has_many: "%{record}が存在しているので削除できません" 10 date: 11 abbr_day_names: 12 - 日 13 - 月 14 - 火 15 - 水 16 - 木 17 - 金 18 - 土 19 abbr_month_names: 20 - 21 - 1月 22 - 2月 23 - 3月 24 - 4月 25 - 5月 26 - 6月 27 - 7月 28 - 8月 29 - 9月 30 - 10月 31 - 11月 32 - 12月 33 day_names: 34 - 日曜日 35 - 月曜日 36 - 火曜日 37 - 水曜日 38 - 木曜日 39 - 金曜日 40 - 土曜日 41 formats: 42 default: "%Y/%m/%d" 43 long: "%Y年%m月%d日(%a)" 44 short: "%m/%d" 45 month_names: 46 - 47 - 1月 48 - 2月 49 - 3月 50 - 4月 51 - 5月 52 - 6月 53 - 7月 54 - 8月 55 - 9月 56 - 10月 57 - 11月 58 - 12月 59 order: 60 - :year 61 - :month 62 - :day 63 datetime: 64 distance_in_words: 65 about_x_hours: 66 one: 約1時間 67 other: 約%{count}時間 68 about_x_months: 69 one: 約1ヶ月 70 other: 約%{count}ヶ月 71 about_x_years: 72 one: 約1年 73 other: 約%{count}年 74 almost_x_years: 75 one: 1年弱 76 other: "%{count}年弱" 77 half_a_minute: 30秒前後 78 less_than_x_seconds: 79 one: 1秒以内 80 other: "%{count}秒未満" 81 less_than_x_minutes: 82 one: 1分以内 83 other: "%{count}分未満" 84 over_x_years: 85 one: 1年以上 86 other: "%{count}年以上" 87 x_seconds: 88 one: 1秒 89 other: "%{count}秒" 90 x_minutes: 91 one: 1分 92 other: "%{count}分" 93 x_days: 94 one: 1日 95 other: "%{count}日" 96 x_months: 97 one: 1ヶ月 98 other: "%{count}ヶ月" 99 x_years: 100 one: 1年 101 other: "%{count}年" 102 prompts: 103 second: 秒 104 minute: 分 105 hour: 時 106 day: 日 107 month: 月 108 year: 年 109 errors: 110 format: "%{attribute}%{message}" 111 messages: 112 accepted: を受諾してください 113 nickname: 114 blank: を入力してください 115 confirmation: と%{attribute}の入力が一致しません 116 empty: を入力してください 117 equal_to: は%{count}にしてください 118 even: は偶数にしてください 119 exclusion: は予約されています 120 greater_than: は%{count}より大きい値にしてください 121 greater_than_or_equal_to: は%{count}以上の値にしてください 122 inclusion: は一覧にありません 123 invalid: は不正な値です 124 less_than: は%{count}より小さい値にしてください 125 less_than_or_equal_to: は%{count}以下の値にしてください 126 model_invalid: 'バリデーションに失敗しました: %{errors}' 127 not_a_number: は数値で入力してください 128 not_an_integer: は整数で入力してください 129 odd: は奇数にしてください 130 other_than: は%{count}以外の値にしてください 131 present: は入力しないでください 132 required: を入力してください 133 taken: はすでに存在します 134 too_long: は%{count}文字以内で入力してください 135 too_short: は%{count}文字以上で入力してください 136 wrong_length: は%{count}文字で入力してください 137 template: 138 body: 次の項目を確認してください 139 header: 140 one: "%{model}にエラーが発生しました" 141 other: "%{model}に%{count}個のエラーが発生しました" 142 helpers: 143 select: 144 prompt: 選択してください 145 submit: 146 create: 登録する 147 submit: 保存する 148 update: 更新する 149 number: 150 currency: 151 format: 152 delimiter: "," 153 format: "%n%u" 154 precision: 0 155 separator: "." 156 significant: false 157 strip_insignificant_zeros: false 158 unit: 円 159 format: 160 delimiter: "," 161 precision: 3 162 separator: "." 163 significant: false 164 strip_insignificant_zeros: false 165 human: 166 decimal_units: 167 format: "%n %u" 168 units: 169 billion: 十億 170 million: 百万 171 quadrillion: 千兆 172 thousand: 千 173 trillion: 兆 174 unit: '' 175 format: 176 delimiter: '' 177 precision: 3 178 significant: true 179 strip_insignificant_zeros: true 180 storage_units: 181 format: "%n%u" 182 units: 183 byte: バイト 184 eb: EB 185 gb: GB 186 kb: KB 187 mb: MB 188 pb: PB 189 tb: TB 190 percentage: 191 format: 192 delimiter: '' 193 format: "%n%" 194 precision: 195 format: 196 delimiter: '' 197 support: 198 array: 199 last_word_connector: "、" 200 two_words_connector: "、" 201 words_connector: "、" 202 time: 203 am: 午前 204 formats: 205 default: "%Y年%m月%d日(%a) %H時%M分%S秒 %z" 206 long: "%Y/%m/%d %H:%M" 207 short: "%m/%d %H:%M" 208 pm: 午後

#試したこと

私の中では、エラー分の内容はcan't be blankが日本語表記になってないという意味と感じました。
deviceの日本語表示を全て消せば解決するらしいですが、日本語表示にしたいです。
下記のように追加してみたのですが、エラー解消出来ません。
単体テストは、初めての経験で分からないことだらけですが、
ご教示いただけますと幸いです。

errors: format: "%{attribute}%{message}" messages: accepted: を受諾してください can't be blank:空白 nickname: blank: を入力してください

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

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

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

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

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

guest

回答1

0

nicknameカラムのバリデーションはどのように設定してあるのでしょうか?
presence:true(nullを許可しない)で設定してある場合は
user = User.new(nickname: "")
と書くとnicknameがnullではなく""(空文字)となり、文字はないもののデータ自体はある(=nullではない)という判定になるのでテストが失敗すると思われます。

おそらくですがUser.new(nickname: nil, 以下略)とすれば行けると思います。

投稿2020/04/26 02:03

ryuuzu

総合スコア30

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問