1
0

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.

結合テストで画像を添付する

Posted at

参考にした記事

結合テストで画像を添付する方法

オリジナルアプリの新規投稿の結合テストコードを実装しようとしたときに添付のやり方がわからなかったので調べました。

books_spec.rb
 image_path = Rails.root.join('public/images/test.png')
 attach_file('book[image]',image_path, make_visible: true)

image_pathという変数にテスト用の画像を添付します。相対パスで指定をするみたいです。
attach_fileメソッドはアップロードのinput要素にテスト用画像を添付することができます。
第1引数:アップロードするinput
要素のname属性の値
第2引数:アップロードする画像のパス
第3引数:オプション(make_visible: trueで一時的に表示)

添付したテスト用画像が表示されているかの確認方法

添付した画像が表示されているかの確認方法です。

books_spec.rb
 expect(page).to have_selector("img[src$='test.png']")

have_selectorで要素があるか判断をします。

あとは必要な他のコードを記述して問題なく実行できたのでOKでした。
ほぼ参考にさせていただいた記事の記述でいけました。

1
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?