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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1823閲覧

Failure/Error: expect(page).to have_no_content("#{drink.name}") expected not to find text

divclass123

総合スコア35

RSpec

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/04/15 17:41

編集2021/04/19 05:43

前提・実現したいこと

投稿した、投稿の削除の結合テストを書いてるのですが、パスできません
このアプリのgithub

docker上で開発していて、クロムをインストールしました

発生している問題・エラーメッセージ

soichirohara@SoichironoMacBook-Pro coffee_passport % docker-compose exec web rspec spec/system/drinks_spec.rb Drinks 投稿ができる時 ログインしたユーザーは新規投稿できる 投稿ができない時 ログインしてないユーザーは新規投稿できない 削除できるとき 投稿した本人ならその投稿を削除できる (FAILED - 1) 削除できない時 投稿した本人ではないので、その投稿が削除できない Failures: 1) Drinks 削除できるとき 投稿した本人ならその投稿を削除できる Failure/Error: expect(page).to have_no_content("#{drink.name}") expected not to find text "TOKYOロースト" in "はらそう\n投稿する\n商品を購入\n検索\nTimeline\nSelected\nはらそう\nTOKYOロースト\n350円(税込み)\nこれはコーヒーの説明です\n0\nはらそう\nTOKYOロースト\n350円(税込み)\nこれはコーヒーの説明です\n0" # ./spec/system/drinks_spec.rb:79:in `block (3 levels) in <top (required)>' Finished in 4.73 seconds (files took 3.89 seconds to load) 4 examples, 1 failure Failed examples: rspec ./spec/system/drinks_spec.rb:59 # Drinks 削除できるとき 投稿した本人ならその投稿を削除できる ERROR: 1 soichirohara@SoichironoMacBook-Pro coffee_passport % docker-compose exec web rspec spec/system/drinks_spec.rb Drinks 投稿ができる時 ログインしたユーザーは新規投稿できる 投稿ができない時 ログインしてないユーザーは新規投稿できない 削除できるとき 投稿した本人ならその投稿を削除できる (FAILED - 1) 削除できない時 投稿した本人ではないので、その投稿が削除できない Failures: 1) Drinks 削除できるとき 投稿した本人ならその投稿を削除できる Failure/Error: expect(page).to have_no_content("TOKYOロースト") expected not to find text "TOKYOロースト" in "はらそう\n投稿する\n商品を購入\n検索\nTimeline\nSelected\nはらそう\nTOKYOロースト\n350円(税込み)\nこれはコーヒーの説明です\n0\nはらそう\nTOKYOロースト\n350円(税込み)\nこれはコーヒーの説明です\n0" # ./spec/system/drinks_spec.rb:79:in `block (3 levels) in <top (required)>' Finished in 4.62 seconds (files took 3.99 seconds to load) 4 examples, 1 failure Failed examples: rspec ./spec/system/drinks_spec.rb:59 # Drinks 削除できるとき 投稿した本人ならその投稿を削除できる ERROR: 1

該当のソースコード

spec/system/drinks_spec.rb

ruby

1 let!(:user) { FactoryBot.create(:user) } 2 let(:drink) { FactoryBot.create(:drink) } 3 let!(:other_user) { FactoryBot.create(:user) } 4 let(:other_drink) { FactoryBot.create(:drink) } 5 6 context '削除できるとき' do 7 it "投稿した本人ならその投稿を削除できる" do 8 9 visit login_path 10 fill_in 'email', with: drink.user.email 11 fill_in 'password', with: drink.user.password 12 find('input[name="commit"]').click 13 expect(current_path).to eq(user_path(drink.user)) 14 15 # 投稿詳細ページに移動 16 visit drink_path(drink) 17 # 削除のリンクがある 18 expect(page).to have_link '削除する', href: drink_path(drink) 19 # 削除したら、Drinkモデルのカウントが1減る 20 expect{ 21 find_link('削除する', href: drink_path(drink)).click 22 }.to change { Drink.count }.by(-1) 23 # トップページに遷移される 24 visit root_path 25 # 削除した投稿はない 26 expect(page).to have_no_content(drink.image) 27 expect(page).to have_no_content("TOKYOロースト") 28 expect(page).to have_no_content("#{drink.price}") 29 expect(page).to have_no_content("#{drink.explain}") 30 end 31 end

factories/drinks.rb

FactoryBot.define do factory :drink do name {"TOKYOロースト"} price {350} explain {"これはコーヒーの説明です"} region_id {2} body_id {Body.all.sample} acidity_id {Acidity.all.sample} processing_id {Processing.all.sample} likes_count {2} association :user association :region association :body association :acidity association :processing after(:build) do |drink| drink.image.attach(io: File.open('app/assets/images/ethiopia.jpg'), filename: 'ethiopia.jpg') end end end

docker-compose.yml

yml

1 web: 2 enviroment: 3 SELENIUM_DRIVER_URL: http://selenium_chrome:4444/wd/hub" 4 selenium_chrome: 5 image: selenium/standalone-chrome-debug 6 logging: 7 driver: none

Dockerfile

# chromeの追加 RUN apt-get update && apt-get install -y unzip && \ CHROME_DRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \ wget -N http://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip -P ~/ && \ unzip ~/chromedriver_linux64.zip -d ~/ && \ rm ~/chromedriver_linux64.zip && \ chown root:root ~/chromedriver && \ chmod 755 ~/chromedriver && \ mv ~/chromedriver /usr/bin/chromedriver && \ sh -c 'wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -' && \ sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' && \ apt-get update && apt-get install -y google-chrome-stable

spec/support/selenium_chrome.rb

require 'capybara/rspec' require 'selenium-webdriver' Capybara.register_driver :selenium_chrome_headless do |app| options = ::Selenium::WebDriver::Chrome::Options.new options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--window-size=1400,1400') driver = Capybara::Selenium::Driver.new(app, browser: :chrome, options: options) end Capybara.javascript_driver = :selenium_chrome_headless

spec/support/capybara.rb

Capybara.register_driver :chrome_headless do |app| options = ::Selenium::WebDriver::Chrome::Options.new options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--window-size=1400,1400') Capybara::Selenium::Driver.new(app, browser: :chrome, options: options) end Capybara.javascript_driver = :chrome_headless RSpec.configure do |config| config.before(:each, type: :system) do driven_by :rack_test end config.before(:each, type: :system, js: true) do if ENV["SELENIUM_DRIVER_URL"].present? driven_by :selenium, using: :chrome, options: { browser: :remote, url: ENV.fetch("SELENIUM_DRIVER_URL"), desired_capabilities: :chrome } else driven_by :selenium_chrome_headless end end end

試したこと

expect(page).to have_no_content("#{drink.name}")

と記載していたところ

expect(page).to have_no_content("TOKYOロースト")

と記載しました。
が、エラー内容は一緒でした。

context '削除できるとき' do it "投稿した本人ならその投稿を削除できる" do visit login_path fill_in 'email', with: drink.user.email fill_in 'password', with: drink.user.password find('input[name="commit"]').click expect(current_path).to eq(user_path(drink.user)) # 投稿詳細ページに移動 visit drink_path(drink) # 削除のリンクがある expect(page).to have_link '削除する', href: drink_path(drink) # 削除したら、Drinkモデルのカウントが1減る expect{ find_link('削除する', href: drink_path(drink)).click }.to change { Drink.count }.by(-1) # トップページに遷移される visit root_path # 削除した投稿はない expect(page).to have_no_content(drink.image) expect(page).to have_content("TOKYOロースト") expect(page).to have_content("#{drink.price}") expect(page).to have_content("#{drink.explain}") end end

このようなテストだと通りました。
画像は削除された?のですが、 expect(page).to have_link '削除する', href: drink_path(drink)
が機能してない?

でも expect{
find_link('削除する', href: drink_path(drink)).click
}.to change { Drink.count }.by(-1)

がパスできてる?

binding.pryを用いて色々やったのですが、なんか効果的なデバッグ方法があればご教授いただけると幸いです。

spec/system/drinks_spec.rb

ruby

1 let!(:user) { FactoryBot.create(:user) } 2 let(:drink) { FactoryBot.create(:drink) } 3 let!(:other_user) { FactoryBot.create(:user) } 4 let(:other_drink) { FactoryBot.create(:drink) } 5

で、

let(:other_drink) { FactoryBot.create(:drink) }

があることで、テストがパスできないと思いコメントアウトしても、同じエラーが起こってしまいます。
削除する。のリンクをクリックするテストはパスできてるはずなので、なぜこのようなエラーが起こってしまうのか分かりません。

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

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

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

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

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

guest

回答1

0

自己解決

投稿を削除した後に、テストの記述では
visit root_pathになってたが、アプリの仕様では、自分の詳細ページにリダイレクトされるような仕様だった。
自分のアプリの仕様の把握不足でした。

投稿2021/04/19 06:26

divclass123

総合スコア35

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問