前提・実現したいこと
投稿型アプリのmodelの単体テストで
bundle exec rspecを実行すると以下のエラーが発生します。
以下のエラー内容を解消してrspec単体テストを実行したいです。
発生している問題・エラーメッセージ
console
1(中略) 2 Failure/Error: note_id {FactoryBot.create(:note).id} 3 4 ActiveRecord::RecordInvalid: 5 Validation failed: Images can't be blank 6(中略)
該当のmodelコード
model/note.rb
ruby
1class Note < ApplicationRecord 2 belongs_to :user 3 has_many :images 4 accepts_nested_attributes_for :images, allow_destroy: true 5 validates :title, :status, :subject, :text ,presence: true 6 validates :images, presence: true 7end
model/image.rb
ruby
1class Image < ApplicationRecord 2 mount_uploader :src, ImageUploader 3 belongs_to :note 4end 5
###該当のspecコード
spec/model/image_spec.rb
require 'rails_helper' describe Image do describe '#create'do it 'note_id(外部キー)が存在すれば投稿可' do image = build(:image) expect(image).to be_valid end end end
spec/model/note_spec.rb
require 'rails_helper' describe Note do describe '#create' do it '必須項目が全て入力できていたら投稿可' do Image = FactoryBot.create(:image).id note = build(:note) expect(note).to be_valid end it 'ノートタイトルがない場合は投稿不可' do note = build(:note, title: nil) note.valid? expect(note.errors[:title]).to include("can't be blank") end it '対象が選択されていない場合は投稿不可' do note = build(:note, status: nil) note.valid? expect(note.errors[:status]).to include("can't be blank") end it '科目が選択されていない場合は投稿不可' do note = build(:note, subject: nil) note.valid? expect(note.errors[:subject]).to include("can't be blank") end it 'ノート説明が記入されていない場合は投稿不可' do note = build(:note, text: nil) note.valid? expect(note.errors[:text]).to include("can't be blank") end end end
試したこと
こちらの記事を参考に外部キーを用いた
単体テストの記入を試みた。
結果、userに関しては解消できた。
しかし、エラーメッセージのImageに関する解消アプローチが分からないままです。
ご意見のほどよろしくお願いします。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。