質問させて頂きます。
現在ruby on railsにてテストコードを書いております。
エラーでは無いのですが、以下の現象が発生しました。
RSpecwo使ったuserモデルのテストコードです。(devise利用)
こちらが問題のテストコードです。
25行目でnicknameを空にしているので通常であればエラーとなるのですが、テストコードを正常に通過してしまいます。
【spec>models>user_spec.rb】
require
1describe User do 2 before do 3 @user = FactoryBot.build(:user) 4 end 5 6 describe 'ユーザー新規登録' do 7 context '新規登録がうまくいくとき' do 8 it "nicknameとemail、passwordとpassword_confirmationが存在すれば登録できる" do 9 expect(@user).to be_valid 10 end 11 it "nicknameが8文字以下で登録できる" do 12 @user.nickname = "aaaaaaaa" 13 expect(@user).to be_valid 14 end 15 it "passwordが6文字以上であれば登録できる" do 16 @user.password = "000000" 17 @user.password_confirmation = "000000" 18 expect(@user).to be_valid 19 end 20 end 21 22 context '新規登録がうまくいかないとき' do 23 it "nicknameが空だと登録できない" do 24 @user.nickname = '' 25 @user.valid? 26 end 27 it "nicknameが9文字以上であれば登録できない" do 28 @user.nickname = 'aaaaabccccccccccccccccbbb' 29 @user.valid? 30 end 31 end 32 end 33end 34コード
以下のように10行目でnicknameカラムにpresence: trueのバリデーションをかけているのですが、テストが正常に通過してしまいます。
【app>models>users.rb】
class
1 # Include default devise modules. Others available are: 2 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 3 devise :database_authenticatable, :registerable, 4 :recoverable, :rememberable, :validatable 5 has_many :nutritions 6 has_many :favorites, dependent: :destroy 7 has_many :fav_nutritions, through: :favorites, source: :nutrition 8 9 validates :nickname, presence: true, length: { maximum: 8 } 10 11 def already_favorited?(nutrition, current_user) 12 Favorite.exists?(user_id: current_user.id, nutrition_id: nutrition.id) 13 end 14end 15 16コード
↓bundle exec rspec spec/models/user_spec.rbコマンドでテストしますがなぜか成功してしまいます。
通常であればnicknameが空の時はエラーになると思うのですが何故なのでしょうか?
ローカル上でのブラウザでは正常にバリデーションがかかっております。(nisknameを空で登録しようとするとエラー文が返る)
ご教示頂けますと幸いです。
回答1件
あなたの回答
tips
プレビュー