Home>

I'm unit testing my model, but I can't resolve the following error in the terminal.
I have tried most of the things I can think of, but I would appreciate it if you could teach me.

User
  #create
    If user can be registered
      name, email, password and password_confirmation exist
      If password is 6 characters or more
    If user cannot be registered
      No name (FAILED -1)
      No email (FAILED-2)
      No password (FAILED -3)
      Even if password exists, there is no password_confirmation (FAILED -4)
      Duplicate emails exist (FAILED-5)
      password must be 5 characters or less (FAILED -6)
Failures:
  1) If User # create user cannot be registered There is no name
     Failure/Error: expect (user.errors [: name]). to include ("can't be blank")
       expected [] to include "can't be blank"
     # ./spec/models/user_spec.rb:22: in `block (4 levels) in<top (required)>'
  2) If User # create user cannot be registered, there is no email
     Failure/Error: expect (user.errors [: email]). to include ("can't be blank")
       expected ["Please enter"] to include "can't be blank"
     # ./spec/models/user_spec.rb:28: in `block (4 levels) in<top (required)>'
  3) If User # create user cannot be registered, there is no password
     Failure/Error: expect (user.errors [: password]). to include ("can't be blank")
       expected ["Please enter"] to include "can't be blank"
     # ./spec/models/user_spec.rb:34:in `block (4 levels) in<top (required)>'
  4) When User # create user cannot be registered Even if password exists, password_confirmation does not exist
     Failure/Error: expect (user.errors [: password_confirmation]). to include ("doesn't match Password")
       expected ["does not match password entry"] to include "doesn't match Password"
     # ./spec/models/user_spec.rb:40:in `block (4 levels) in<top (required)>'
  5) If User # create user cannot be registered There must be duplicate emails
     Failure/Error: expect (another_user.errors [: email]). to include ("has already been taken")
       expected ["has already been taken"] to include "has already been taken"
     # ./spec/models/user_spec.rb:49:in `block (4 levels) in<top (required)>'
  6) If User # create user cannot be registered password must be 5 characters or less
     Failure/Error: expect (user.errors [: password]). to include ("is too short (minimum is 6 characters)")expected ["Please enter at least 6 characters"] to include "is too short (minimum is 6 characters)"
     # ./spec/models/user_spec.rb:55:in `block (4 levels) in<top (required)>'
Finished in 0.40026 seconds (files took 5.01 seconds to load)
8 examples, 6 failures
Failed examples:
rspec ./spec/models/user_spec.rb:19 # User # create If user cannot be registered There is no name
rspec ./spec/models/user_spec.rb:25 # User # create If user cannot be registered There is no email
rspec ./spec/models/user_spec.rb:31 # User # create If user cannot be registered There is no password
rspec ./spec/models/user_spec.rb:37 # User # create If user cannot be registered There is no password_confirmation even if password exists
rspec ./spec/models/user_spec.rb:43 # User # create If user cannot be registered Duplicate email exists
rspec ./spec/models/user_spec.rb:52 # User # create If user cannot be registered password must be 5 characters or less
FactoryBot.define do
  factory: user do
    password = Faker :: Internet.password (min_length: 8)
    name {Faker :: Name.last_name}
    email {Faker :: Internet.free_email}
    password {password}
    password_confirmation {password}
  end
end
require'rails_helper'
RSpec.describe User, type:: model do
  describe'#create' do
    context'if user can be registered' do
      it "name, email, password and password_confirmation exist" do
        user = build (: user)
        expect (user) .to be_valid
      end
      it "If password is 6 characters or more" do
        user = build (: user, password: "000000", password_confirmation: "000000")
        expect (user) .to be_valid
      end
    end

    context'If user cannot be registered' do
      it "no name" do
        user = build (: user, name: nil)user.valid?
        expect (user.errors [: name]). to include ("can't be blank")
      end
      it "no email" do
        user = build (: user, email: nil)
        user.valid?
        expect (user.errors [: email]). to include ("can't be blank")
      end
      it "no password" do
        user = build (: user, password: nil)
        user.valid?
        expect (user.errors [: password]). to include ("can't be blank")
      end
      it "password exists but password_confirmation does not exist" do
        user = build (: user, password_confirmation: "")
        user.valid?
        expect (user.errors [: password_confirmation]). to include ("doesn't match Password")
      end
      it "Duplicate emails exist" do
        #First register user
        user = create (: user)
        #Create an instance of a user with the same email value as the previously registered user
        another_user = build (: user, email: user.email)
        another_user.valid?
        expect (another_user.errors [: email]). to include ("has already been taken")
      end
      it "password must be 5 characters or less" do
        user = build (: user, password: "00000", password_confirmation: "00000")
        user.valid?
        expect (user.errors [: password]). to include ("is too short (minimum is 6 characters)")
      end
    end
  end
end

``

  • Answer # 1

    For 2 to 6, write the error in Japanese.
    * This is because the app has been translated into Japanese.

    Isn't 1 probably forgotten to write validates?

Related articles