前提・実現したいこと
ruby 2.6.5
Rails 6.0.4.1
現在作成中のアプリケーションにてActiveStrageを使った画像複数投稿機能を実装しています。
モデルテストにあたりRSpecを使っておりますが、「Images can't be blank」のエラーメッセージが出てしまいテストが通りません。
下記情報以外に必要な情報ございましたら追記いたします。
お詳しい方いらっしゃいましたら、ご教授いただけますと助かります。
発生している問題・エラーメッセージ
expected #<Store id: nil, name: "[\"laboriosam\", \"odio\", \"veniam\"]", address: "Levichester", postal_code: "51926 McKenzie Stream", telephone: "646-435-7381", url: "http://hauck.biz/latonya_runolfsdottir", closing_day: "Non dolores qui.", business_hour: "21", fee: 1867, parking_car: "[\"enim\", \"quo\", \"tempora\"]", parking_bicycle: "[\"tempora\", \"ut\", \"impedit\"]", towel_rental: "[\"et\", \"impedit\", \"quia\"]", water: "49", water_depth: "4", water_quality: "[\"optio\", \"vel\", \"et\"]", temperature: 95, roryu_status: "[\"ratione\", \"dolores\", \"consequuntur\"]", roryu_time: "17", air_bath: "[\"velit\", \"delectus\", \"voluptate\"]", break_place: "[\"deserunt\", \"eligendi\", \"fuga\"]", television: "[\"eum\", \"quibusdam\", \"quod\"]", bgm: "[\"rerum\", \"consequatur\", \"reprehenderit\"]", user_id: nil, created_at: nil, updated_at: nil> to be valid, but got errors: Images can't be blank
該当のソースコード
store.rb
ruby
1class Store < ApplicationRecord 2 has_many :comments, dependent: :destroy 3 has_many :goods, dependent: :destroy 4 belongs_to :user 5 has_many_attached :images 6 7 with_options presence: true do 8 validates :name, :address, :postal_code, :telephone, :url, 9 :closing_day, :business_hour, :fee, :parking_car, :parking_bicycle, :towel_rental, :water, :water_depth, :water_quality, 10 :temperature, :roryu_status, :roryu_time, :air_bath, :break_place, :television, :bgm, :water_depth, :images 11 end 12 13 def favorited_by?(user) 14 goods.where(user_id: user.id).exists? 15 end 16end
store_spec.rb
ruby
1require 'rails_helper' 2 3RSpec.describe Store, type: :model do 4 before do 5 @store = FactoryBot.build(:store) 6 @store.images = fixture_file_upload('/300x300.png') 7 end 8 9 describe '店舗情報の投稿' do 10 context '店舗情報が投稿できる' do 11 it '必要な情報が全て入力されていれば投稿できる' do 12 expect(@store).to be_valid 13 end 14 end 15 (中略) 16end
stores.rb
FactoryBot.define do factory :store do name {Faker::Lorem.words} address {Faker::Address.city} postal_code {Faker::Address.street_address} telephone {Faker::PhoneNumber.cell_phone} url {Faker::Internet.url} closing_day {Faker::Lorem.sentence(word_count: 3)} business_hour {Faker::Number.within(range: 1..24)} fee {Faker::Number.within(range: 1..10000)} parking_car {Faker::Lorem.words} parking_bicycle {Faker::Lorem.words} towel_rental {Faker::Lorem.words} water {Faker::Number.within(range: 1..100)} water_depth {Faker::Number.within(range: 1..100)} water_quality {Faker::Lorem.words} temperature {Faker::Number.within(range: 1..100)} roryu_status {Faker::Lorem.words} roryu_time {Faker::Number.within(range: 1..24)} air_bath {Faker::Lorem.words} break_place {Faker::Lorem.words} television {Faker::Lorem.words} bgm {Faker::Lorem.words} association :user end end
あなたの回答
tips
プレビュー