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

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

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

Capybaraは、 Rubyで開発されているWebアプリケーションテストフレームワークです。Webブラウザ不要でブラウザ上のユーザー操作及びJavaScriptの挙動を自動化することができます。

RSpec

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

Ruby on Rails

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

Q&A

解決済

1回答

3327閲覧

RSpec・Capybaraを使用した、特定の要素にクラス名が付与されていることをテストする方法

yukihide1188

総合スコア44

Capybara

Capybaraは、 Rubyで開発されているWebアプリケーションテストフレームワークです。Webブラウザ不要でブラウザ上のユーザー操作及びJavaScriptの挙動を自動化することができます。

RSpec

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/09/29 12:06

編集2020/10/03 08:01

前提・実現したいこと

RSpecのsystem specで、特定の要素にjQueryで新たにクラス名が付与されることをテストしたいのですがうまくできません。アドバイスいただけないでしょうか。

Railsでformを作成し、そのバリデーションエラーをjQueryで表示できるようにしました。

Haml

1= form_with model: @user do |f| 2 .last-name 3 = f.label :last_name, '名字', id: 'last_name-label' 4 = f.text_field :last_name, id: 'user-last_name' 5 6 = f.label :first_name, '名前', id: 'first_name-label' 7 = f.text_field :first_name, id: 'user-first_name'

jQuery

1$('#user-last_name').blur(function(){ 2 if($(this).val() == '') { 3 $(this).prev('label').addClass('error-label'); 4 $(this).addClass('error-frame'); 5 $(this).after(`<div id="name-blank-error" class="error-message">入力してください</div>`); 6 7 } else { 8 $(this).prev('label').removeClass('error-label'); 9 $(this).removeClass('error-frame'); 10 $(this).next().remove('.error-message'); 11 }; 12});

scss

1.error-label { 2 color: red; 3} 4 5.error-frame { 6 border: solid 3px red; 7}

そして、system specaddClassされていることをテストしようとしたのですが、通りませんでした。

Rspec

1describe 'name' do 2 context '空の場合' do 3 context 'フォーカスが外れた時' do 4 before do 5 fill_in 'user[last_name]', with: '' 6 find('#user-first_name).click 7 end 8 9 it { expect(find('#last_name-label')).to have_css '.error-label' } 10 11 it {expect(find('#user-last_name')).to have_css '.error-frame' } 12 end 13 end 14end

テスト結果

Failure/Error: it { expect(find('#last_name-label')).to have_css '.error-label' } expected to find css ".error-label" within #<Capybara::Node::Element tag="label" path="/HTML/BODY[1]/DIV[1]/DIV[2]/DIV[2]/FORM[1]/DIV[1]/LABEL[1]"> but there were no matches Failure/Error: it { expect(find('#user-last_name')).to have_css '.error-frame' } expected to find css ".error-frame" within #<Capybara::Node::Element tag="input" path="/HTML/BODY[1]/DIV[1]/DIV[2]/DIV[2]/FORM[1]/DIV[1]/INPUT[1]"> but there were no matches

試したこと

  • expect(find('#last_name-label')).toの( )内をpageにしてみるとテストは通りました。
it { expect(page)).to have_css '.error-label' }
  • テスト失敗時のスクリーンショットを確認すると、クラス名は付与され、ラベルやフォームの枠はしっかり赤くなっていました。
  • have_csshave_selectorにしてみましたが、結果は同じで通りませんでした。
it { expect(find('#last_name-label')).to have_selector '.error-label' }
  • expect(find('#last_name-label')).toの()内のfindallに変更してみましたが、こちらもテストは通りませんでした。
it { expect(all('#last_name-label')).to have_selector '.error-label' } it { expect(all(:css, '#last_name-label')).to have_selector '.error-label' }
Failure/Error: it { expect(all(:css, '#last_name-label')).to have_selector '.error-label' } expected to find css ".error-label" but there were no matches Failure/Error: it { expect(all('#last_name-label')).to have_selector '.error-label' } expected to find css ".error-label" but there were no matches

findallは( )内の要素を探してくれると思っていたのですが、使い方や考え方が間違っているのでしょうか?

補足情報(FW/ツールのバージョンなど)

rails (6.0.3.2)
ruby (2.7.1)
rspec-rails (4.0.1)
capybara (3.33.0)
selenium-webdriver (3.142.7)

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

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

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

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

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

guest

回答1

0

自己解決

解決しました。
Module: Capybara::Node::Matchers
Module: Capybara::Node::Finders
を参考に、

Rspec

1it { expect(page).to have_selector '#last_name-label', class: 'error-label' } 2 3it { expect(page).to have_selector '#user-last_name', class: 'error-frame' }

でテストが通りました。

投稿2020/10/03 08:06

yukihide1188

総合スコア44

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問