3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【CarrierWave】FactoryBotでDBに画像を登録したい場合の記述

Posted at

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>'
3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?