CarrierWaveを使用した画像登録用の項目において、Rspec実行時にFactoryBotにてデータを登録したい場合は以下のように記載する。
/spec/factories/users.rb
FactoryBot.define do
factory :user, class: User do
name { 'Example User' }
password { 'foobar' }
password_confirmation { 'foobar' }
image { File.open(File.join(Rails.root, 'spec/fixtures/files/test_pic_01.jpg')) }
# image { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixtures/files/test_pic_01.jpg'), 'image/jpg') }
end
end
※元々コメントアウトしている行の記載で実行できていたが、どこかのタイミングでエラーが発生するようになってしまい、原因を追いきれなかったため、修正した。
エラー内容
ActiveRecord::RecordInvalid:
Validation failed: Picture Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: `magick convert /app/tmp/1619517646-472723409015039-0001-1669/test_pic_01.jpg -auto-orient -resize 800x800> /tmp/image_processing20210427-422-hd5w3e.jpg` failed with error:
convert: insufficient image data in file `/app/tmp/1619517646-472723409015039-0001-1669/test_pic_01.jpg' @ error/jpeg.c/ReadJPEGImage_/1104.
convert: no images defined `/tmp/image_processing20210427-422-hd5w3e.jpg' @ error/convert.c/ConvertImageCommand/3301.
# ./spec/system/show_shop_spec.rb:10:in `block (2 levels) in <main>'
# ./spec/system/show_shop_spec.rb:32:in `block (2 levels) in <main>'
# -e:1:in `<main>'