LoginSignup
0
1

More than 3 years have passed since last update.

画像を用いた単体テストコードをする場合の画像挿入方法

Posted at

結論

publicディレクトリの中に、imagesディレクトリを作り、その中に画像を挿入する。

過程

商品を出品する際に、画像がないと登録できない、というテストをしようとした。
その際に、まずfakerを導入。
そして、下記のように記述。

spec/factories/items.rb
 after(:build) do |item|
      item.image.attach(io: File.open('public/images/test_image.png'),filename: 'test_image.png')
    end 

この記述をすることで、テスト時に画像を自動で挿入してくれる。

注意点

画像を保存したときの名前の通りに、ファイルの名前を入れないと読み込んでくれないこと。
また、使用する画像は、PCのなかに、取り込まないといけないこと。

0
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
0
1