LoginSignup
0
0

More than 3 years have passed since last update.

RSpecを使ってテストコードを書いているが、うまくいきません

Posted at

はじめに

 この記事を読んでも、何かの解決に繋がることはありませんので、ご容赦ください。個人的な悩みのアウトプットです。

今の悩み

 Fackerを使ってユーザーのテスト用の情報をビルドして、ちゃんと保存することができるかのテストを行っています。

課題

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 'ユーザー登録' do
    before do
      @school = FactoryBot.create(:school)
      @class_room = FactoryBot.create(:class_room)
      @user = FactoryBot.build(:user)
    end
    it '全ての項目が入力されていれば、登録できること' do
       expect(@user).to be_valid
    end
  end
end

「全ての項目が入力されていれば、登録できること」これが保存できない!!!!

  • 仮説1
    外部キー(class_room_id)にしているとアソシエーションしていても、ダメなのか。

  • 仮説2
    ヘルパーメソッドでnumber_field(class_room_id)に直接、値を入れてはダメなのか。

最後に

 相談しているわけではないので、数少ないコードだと思うのですが、もしエラーの原因が分かる方がいましたら、ご教授ください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0