[RAILS] I'm writing test code using RSpec, but it doesn't work

Introduction

Please forgive me as reading this article does not lead to any solution. It is an output of personal worries.

Now worries

I'm using Facker to build user test information and test if it can be saved properly.

Task

factories/users.rb



FactoryBot.define do
  factory :user do
    Faker::Config.locale = :ja
    class_room_id { 1 }
    email { Faker::Internet.free_email }
    first_name { Faker::Name.first_name }
    last_name { Faker::Name.last_name }
    nickname { Faker::Name.last_name }
    attendance_number { Faker::Number.between(from: 1, to: 40) }
    password = Faker::Internet.password(min_length: 6)
    password { password }
    password_confirmation { password }

  end
end

user_spec.rb


require 'rails_helper'

RSpec.describe User, type: :model do
  describe 'user registration' do
    before do
      @school = FactoryBot.create(:school)
      @class_room = FactoryBot.create(:class_room)
      @user = FactoryBot.build(:user)
    end
    it 'If all items are entered, you can register' do
       expect(@user).to be_valid
    end
  end
end

"If all items are entered, you can register." This cannot be saved! !! !! !!

--Hypothesis 1 Is it useless even if I associate it with a foreign key (class_room_id)?

--Hypothesis 2 Is it not good to enter the value directly in number_field (class_room_id) with the helper method?

Finally

I haven't consulted with you, so I think it's one of the few codes, but if anyone knows the cause of the error, please teach me.

Recommended Posts

I'm writing test code using RSpec, but it doesn't work
[Rails] Test code using Rspec
RSpec test code execution
Check if you're using Databinding and it doesn't work
"RSpec doesn't work!" The cause was spring, so I investigated it.
Image upload using CarrierWave ~ Rspec test ~
[RSpec] How to write test code
[RSpec] Unit test (using gem: factory_bot)
[Rspec] Flow from introducing Rspec to writing unit test code for a model
Introduce RSpec and write unit test code
I put composer, but it doesn't respond.
After introducing RSpec, until you start writing unit test code for your model