質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

4714閲覧

Rspecの画像アップロードのテストが通らない

Kiyobun510

総合スコア12

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/09/05 08:14

Rspecの画像アップロードのテストがとりません。
テストしたい内容は編集ページでアップロードした画像がマイページ、投稿詳細ページで同じものが表示されているかどうかという内容です。

user successfully upload image user#showは通るのですが、二つ目のuser successfully upload image on item#showが通りません。
ブラウザでは正常に確認できるのでテストコードがおかしいのかもしれません。

投稿詳細ページのユーザーネームにAliceが確認できるのでアソシエーションはうまくいっていると思います。
本来ならitemに紐づけられたAliceのプロフィール画像が設定されないといけないのすが、デフォルトの画像をテストコードで設定するとテストがパスしてしまうという状況です。
(**expect(page).to have_selector("img[src$='test.jpg']")**を
**expect(page).to have_selector("img[src$='noimage.jpg']")**に変更するとテストがパスしてしまいます。)

ご教授いただけますと幸いです。

エラー文

ruby

1 user successfully upload image on item#show (FAILED - 1) 2 3Failures: 4 5 1) UploadImage user successfully upload image on item#show 6 Failure/Error: expect(page).to have_selector("img[src$='test.jpg']") 7 expected to find css "img[src$='test.jpg']" but there were no matches 8 # ./spec/features/upload_image_spec.rb:24:in `block (2 levels) in <main>' 9 10Finished in 11.01 seconds (files took 1.92 seconds to load) 111 example, 1 failure 12 13Failed examples: 14 15rspec ./spec/features/upload_image_spec.rb:20 # UploadImage user successfully upload image on item#show

spec/feature/upload_image_spec.rb

ruby

1require 'rails_helper' 2 3RSpec.feature 'UploadImage', type: :feature do 4 let(:user) { FactoryBot.create(:user) } 5 let(:item) { FactoryBot.create(:item) } 6 7 #画像をアップロードして保存する 8 def upload_user_avatar(user) 9 valid_login(user) 10 visit edit_user_path(user) 11 attach_file 'user_avatar', "#{Rails.root}/spec/fixtures/images/test.jpg" 12 click_on '保存する' 13 end 14 15 scenario 'user successfully upload image user#show' do 16 upload_user_avatar(user) 17 expect(page).to have_selector("img[src$='test.jpg']") 18 end 19 20 scenario 'user successfully upload image on item#show' do 21 upload_user_avatar(user) 22 visit item_path(item) 23 expect(page).to have_content "Alice" 24 expect(page).to have_selector("img[src$='test.jpg']") 25 end 26end

factories/item.rb

ruby

1FactoryBot.define do 2 factory :item do 3 title { 'testtitle' } 4 content { 'testcontent' } 5 user_id { '1' } 6 region { '東京' } 7 association :user 8 end 9end

factories/user.rb

FactoryBot.define do factory :user do nickname { 'Alice' } sequence(:email) { |n| "exemple#{n}@example.com" } password { 'password' } activated { true } end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

unhappychoice

2020/09/05 08:33 編集

すべてのコード見える状態で、デバッグ作業をしないと原因はわからないと思っていて、 `expect(page).to have_content "Alice"` の後で、 - その時点のスクリーンショットを保存 - https://qiita.com/g-fujioka/items/091c400814800f1280ff のような感じ - その時点のHTML を保存 - その時点の DB の様子をみる ( `p User.all` などで ということをしたい気がしますmm
Kiyobun510

2020/09/05 10:19 編集

ご回答ありがとうございます。 現在featureスペックからsystemスペックに乗り換えて同じテストを実行したのですが、tmp/にスクリーンショットが保存されないという問題が発生してしまい原因を調査中です。 ちなみにテスト結果は同じエラーが発生しました。↓ UploadImage /usr/local/bundle/gems/rspec-rails-4.0.0.beta3/lib/rspec/rails/example/system_example_group.rb:91: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call /usr/local/bundle/gems/actionpack-6.0.3.2/lib/action_dispatch/system_test_case.rb:167: warning: The called method `driven_by' is defined here DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from block (2 levels) in <main> at /webapp/spec/system/upload_image_spec.rb:4) 2020-09-05 10:11:45 WARN Selenium [DEPRECATION] :args or :switches is deprecated. Use Selenium::WebDriver::Chrome::Options#add_argument instead. Capybara starting Puma... * Version 4.3.5 , codename: Mysterious Traveller * Min threads: 0, max threads: 4 * Listening on tcp://127.0.0.1:42333 /usr/local/bundle/gems/sprockets-rails-3.2.1/lib/sprockets/rails/helper.rb:355: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call /usr/local/bundle/gems/sprockets-4.0.2/lib/sprockets/base.rb:118: warning: The called method `[]' is defined here DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from update at /webapp/app/controllers/users_controller.rb:61) DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from update at /webapp/app/controllers/users_controller.rb:62) user successfully upload image user#show DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from block (2 levels) in <main> at /webapp/spec/system/upload_image_spec.rb:4) DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from update at /webapp/app/controllers/users_controller.rb:61) DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from update at /webapp/app/controllers/users_controller.rb:62) DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from block (2 levels) in <main> at /webapp/spec/system/upload_image_spec.rb:5) user successfully upload image on item#show (FAILED - 1) Failures: 1) UploadImage user successfully upload image on item#show Failure/Error: expect(page).to have_selector("img[src$='test.jpg']") expected to find css "img[src$='test.jpg']" but there were no matches [Screenshot]: /webapp/tmp/screenshots/failures_r_spec_example_groups_upload_image_user_successfully_upload_image_on_item#show_128.png # ./spec/system/upload_image_spec.rb:23:in `block (2 levels) in <main>'
unhappychoice

2020/09/05 10:20

/webapp/tmp/screenshots/failures_r_spec_example_groups_upload_image_user_successfully_upload_image_on_item#show_128.png に保存されているように見えます
Kiyobun510

2020/09/05 10:25

ありがとうございます。 確かに、このように書いてあるのですが、当該のフォルダをみると何もないという状態で謎です、、 Dockerを使っているのでマウントがうまくいっていないのかもしれないです。 色々と調べてうまくいかないようでしたらまた、別で質問しようと思います。
guest

回答1

0

自己解決

ruby

1let(:item) { FactoryBot.create(:item) }

この部分を

ruby

1let(:item) { FactoryBot.create(:item, user_id: user.id) }

と投稿者を明示することでテストが通りました。

FacrotyBotでアソシエーションを定義していたので投稿したユーザーはuser.idになっているものだと思っていたのですがどうやらそうではないみたいです。頭の片隅に入れておきたいと思います。

回答してくださった方、ありがとうございました。

投稿2020/09/07 09:20

Kiyobun510

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問