Skip to content

ヘッドレスだと失敗するテストを修正#9025

Merged
komagata merged 3 commits intomainfrom
bug/only-pass-headful
Aug 14, 2025
Merged

ヘッドレスだと失敗するテストを修正#9025
komagata merged 3 commits intomainfrom
bug/only-pass-headful

Conversation

@sjabcdefin
Copy link
Copy Markdown
Contributor

@sjabcdefin sjabcdefin commented Aug 12, 2025

Issue

概要

  • 環境により以下のテストがヘッドレスだと失敗するため、ヘッドレスでもテストが通るように修正。

    • rails test test/system/page/tags_test.rb:98
    • rails test test/system/questions_test.rb:123
    • rails test test/system/questions_test.rb:137
  • 私のローカル環境では、修正前のテストでもヘッドレスで成功。そのため、テストが通らない環境をお持ちの @mousu-a さんにご協力いただき、修正後のテストが成功することを確認済み。

修正内容詳細

  • 以下のエラーと失敗に対応する形で修正。

1. Error::UnexpectedAlertOpenError: unexpected alert open: {Alert text : already exists}

  • "already exists" は、同じタグが複数入力された際に、Tagify が出力するアラート。
  • なぜ、同じタグが複数入力されたと判断されたのかは分からなかったが、不正タグの入力→不正タグの削除→正しいタグの入力の過程で出力されていると想定。
  • 対策として、不正タグ入力後に、assert_no_selector('.tagify__tag')を追加し、不正タグが削除されたことを確認した上で正しいタグの入力が確実に行われるよう修正。

2. Failure: Expected: ["foo"], Actual: []

  • 画面(DOM)へのタグ"foo"の反映は完了しているものの、フォーム送信データinput[name="question[tag_list]"] への反映に時間がかかり、その過程で一時的に[登録する]ボタンが disabled の状態になり、クリックできていないことが原因と推測。
  • 対策として、タグ入力後に、assert_selector('input[name="question[tag_list]"][value="foo"]', visible: false)を追加し、フォーム送信データにタグが反映されていることを確認するよう修正。

3. assert_equalassert_selectorに変更

  • assert_equalはMinitestのメソッドであり、要素が出現するまで待機しない。
  • タグの入力を確実に確認するため、assert_selectorに修正。

その他

変更確認方法

  1. bug/only-pass-headfulをローカルに取り込む
  2. ローカル環境で以下のコマンドを実行し、テストが落ちないことを確認する
    • rails test test/system/page/tags_test.rb
    • rails test test/system/questions_test.rb
    • rails test test/system/markdown_test.rb
  3. 本PRのCIで対象テスト(2に記載のテスト)が落ちていないことを確認する

Screenshot

FLAKY なテストの修正であり、見た目の変更はないためスクリーンショットはありません。

Summary by CodeRabbit

  • Tests
    • システムテストに前提初期化を追加し、プロフィール画像関連のテスト安定性を向上。
    • 作成フローのタグ入力検証を厳格化:無効タグが表示されないこと、正しいタグが隠し入力に保存されることを確認。
    • 「質問」と「ページ」作成テストで送信後に該当タグが1件のみ表示されることを明確に検証。

FLAKY なテストになっていたため、assert_selector 等を使用し、要素の状態を確認してから後続処理を実行するように修正
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Aug 12, 2025

Walkthrough

システムテストのみの変更。test/system/markdown_test.rbでavatarリセットの前処理を追加し、test/system/page/tags_test.rbtest/system/questions_test.rbでTagifyの無効タグ検証を厳密化して隠し入力と送信後のDOMアサーションを追加。

Changes

Cohort / File(s) Change Summary
Markdown テストの前提条件追加
test/system/markdown_test.rb
2つのテスト(speak block / user profile image)でusers(:mentormentaro)を読み込みreset_avatar(users(:mentormentaro))をテスト冒頭に追加してからページ遷移する前処理を追加。
Tagify 関連テスト更新(ページ作成)
test/system/page/tags_test.rb
無効タグ入力後に.tagify__tagが描画されないことを確認し、有効タグfoo入力後に隠し入力input[name="page[tag_list]"][value="foo"](visible: false)を確認。送信後は.tag-links__item-linkfooで1件であることを厳密に検証するよう変更。
Tagify 関連テスト更新(質問作成)
test/system/questions_test.rb
上記と同様の検証を質問作成フローの該当テストに適用(無効タグでタグ未描画、隠し入力input[name="question[tag_list]"][value="foo"]確認、送信後fooタグが1件であることを検証)。

Sequence Diagram(s)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers

  • komagata

Poem

ぴょんと跳ねてタグを数える
ぽちっと隠し入力も見つけたよ
空白タグ?それはにんじんじゃないみたい
アバター直してページへごあいさつ
テスト畑に今日も合格の芽が出た 🥕✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 RuboCop (1.76.1)
test/system/markdown_test.rb

rubocop-minitest extension supports plugin, specify plugins: rubocop-minitest instead of require: rubocop-minitest in /.rubocop.yml.
For more information, see https://docs.rubocop.org/rubocop/plugin_migration_guide.html.
rubocop-capybara extension supports plugin, specify plugins: rubocop-capybara instead of require: rubocop-capybara in /.rubocop.yml.
For more information, see https://docs.rubocop.org/rubocop/plugin_migration_guide.html.
Unable to find gem rubocop-fjord; is the gem installed? Gem::MissingSpecError
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:309:in rescue in gem_config_path' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:293:in gem_config_path'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:84:in block (2 levels) in resolve_inheritance_from_gems' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:82:in reverse_each'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:82:in block in resolve_inheritance_from_gems' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:76:in each_pair'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:76:in resolve_inheritance_from_gems' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader.rb:58:in load_file'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_store.rb:29:in options_config=' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:162:in act_on_options'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:47:in block in run' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:82:in profile_if_needed'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:43:in run' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/exe/rubocop:19:in <top (required)>'
/usr/local/bin/rubocop:25:in load' /usr/local/bin/rubocop:25:in

'


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f90681c and 6465df4.

📒 Files selected for processing (1)
  • test/system/markdown_test.rb (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/system/markdown_test.rb
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build_and_test
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bug/only-pass-headful

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@sjabcdefin sjabcdefin force-pushed the bug/only-pass-headful branch from 51a565e to f90681c Compare August 13, 2025 07:46
@sjabcdefin
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Aug 13, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
test/system/markdown_test.rb (1)

7-8: 重複前処理のDRY化(軽微)

同じ2行が2テストで重複しています。可読性向上のため軽くまとめるとよいです(どちらでも可)。

最小限の置換(変数を介さずに1行で呼び出す):

-    user = users(:mentormentaro)
-    reset_avatar(user)
+    reset_avatar users(:mentormentaro)

両テストで使う小ヘルパーをこのクラスに追加する案(参考):

private

def reset_mentor_avatar
  reset_avatar users(:mentormentaro)
end

呼び出し側(各テスト内)は以下のように1行化できます:

reset_mentor_avatar

Also applies to: 24-25

test/system/page/tags_test.rb (1)

114-124: 同様の厳密さを「ドットのみ無効タグ」テストにも適用することを検討(任意)

上のテストと同じく、無効入力後の.tagify__tag不在と、page[tag_list] hidden inputの値確認まで行うと一貫性が高まります。

   test 'alert when enter one dot only tag on creation page' do
     visit_with_auth new_page_path, 'kimura'
     fill_in_tag_with_alert '.'
+    assert_no_selector('.tagify__tag')
     fill_in_tag 'foo'
+    assert_selector('input[name="page[tag_list]"][value="foo"]', visible: false)
     within('form[name=page]') do
       fill_in('page[title]', with: 'test title')
       fill_in('page[body]', with: 'test body')
       click_button 'Docを公開'
     end
     assert_equal ['foo'], all('.tag-links__item-link').map(&:text).sort
   end
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 30a74a9 and f90681c.

📒 Files selected for processing (3)
  • test/system/markdown_test.rb (2 hunks)
  • test/system/page/tags_test.rb (1 hunks)
  • test/system/questions_test.rb (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.rb

⚙️ CodeRabbit Configuration File

**/*.rb: # refactoring

  • まずFat Controllerを避け、次にFat Modelを避ける。
  • Serviceクラスの乱用を避ける。
  • controller concernを作ろうとしたらPORO(Plain Old Ruby Object)やActiveRecordモデルでの実装で代替できないか検討する。

Rails Patterns

  • ViewHelperにメソッドを実装する時にはまずDecoratorパターンを使うことを検討する。(active_decorator gemを導入しているのでそれを使う)
  • 複雑なActiveRecordクエリがあり、再利用できそうな場合はQueryObjectパターンを検討する。(rails-patterns gemを導入しているのでそれのQuery機能を使う)
  • Viewにpartialを作る場合はViewComponentを使うことを検討する。
  • 複数のActiveRecordモデルを操作する1つの責務がある時や外部APIとやりとりする処理がある場合にはInteractorオブジェクトパターンを検討する。(interactor gemを導入しているのでそれを使う)
  • 複数のInteractorを実行するような処理がある場合Organizerオブジェクトパターンを検討する。(interactor gemを導入しており、その中にOrganizerの機能があるので使う)

Files:

  • test/system/markdown_test.rb
  • test/system/page/tags_test.rb
  • test/system/questions_test.rb
test/**/*

⚙️ CodeRabbit Configuration File

test/**/*: # Test

  • TestCase名は英語で書く。
  • どうしても避けられない時以外にsystem testでsleepは使わない。

Unit Test

model, helper, decorator, view_componentについてはメソッドを追加した場合は必ず対応したUnit TestのTestCaseを1つは書く。

Files:

  • test/system/markdown_test.rb
  • test/system/page/tags_test.rb
  • test/system/questions_test.rb
🧬 Code Graph Analysis (2)
test/system/markdown_test.rb (1)
test/supports/avatar_helper.rb (1)
  • reset_avatar (6-14)
test/system/questions_test.rb (1)
test/system/page/tags_test.rb (1)
  • test (5-151)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build_and_test
🔇 Additional comments (4)
test/system/markdown_test.rb (1)

7-8: ヘッドレス環境での安定化のための前処理追加は妥当です

フィクスチャからアバターを明示的にアタッチすることで、ネットワーク依存やデフォルト画像分岐による不安定さを排除できています。意図に合致しており良い修正です。

test/system/questions_test.rb (2)

127-127: 作成フロー(スペース含む無効タグ)の検証強化が適切です

  • 無効入力後に.tagify__tag不在を確認
  • 有効入力時に非表示のhidden inputへ確実に反映されていることを確認
  • 投稿後にタグリンクの出現を件数まで厳密に検証

ヘッドレス差異を吸収できる堅牢なアサーションになっています。

Also applies to: 130-130, 136-136


142-142: 作成フロー(ドットのみ無効タグ)の検証強化も適切です

無効入力のDOM不在確認、hidden inputの値確認、投稿後の件数検証まで一貫しており、ヘッドレスでも安定する構成です。

Also applies to: 144-144, 150-150

test/system/page/tags_test.rb (1)

102-102: ページ作成時のタグ入力検証をヘッドレスに強くした点は良いです

  • 無効入力時の.tagify__tag不在確認
  • 有効入力時のhidden inputの値確認(visible: false)
  • 投稿後のタグリンク出現の件数検証

TagifyのUIと送信値の両面を押さえた堅牢なテストになっています。

Also applies to: 105-105, 111-111

@sjabcdefin sjabcdefin marked this pull request as ready for review August 13, 2025 10:39
@github-actions github-actions bot requested a review from komagata August 13, 2025 10:39
@sjabcdefin
Copy link
Copy Markdown
Contributor Author

@komagata
お疲れ様です。
お手すきの際に、レビューをお願いいたします。😌🙏

2点ご連絡があります。

  • 修正後のテスト
    FLAKYなテストの修正のため、複数人の環境で修正後のテストをしたほうがよいと考え、@ayu-0505 さんにテストのご協力を依頼中です。
    一部テストがHEADFULでしか通らない #8375 (comment)

  • rails test test/system/page/tags_test.rb:98 について
    現状、@mousu-a さんの環境でしか失敗が報告されていません。
    相談タイムの際に、一人の環境でしか起こらない場合は、ローカルな問題として修正しなくて問題ないとお伺いしました。
    そのため、Discordで他の方の環境でも同様の失敗が起こるか確認する予定です。
    (すみません。この後すぐ確認します。)

Comment on lines +7 to +8
user = users(:mentormentaro)
reset_avatar(user)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
user = users(:mentormentaro)
reset_avatar(user)
reset_avatar(users(:mentormentaro))

1箇所でしか出てこないのであればこれでいいかもしれません。また、複数のテストケースで出てくるのであればsetupブロックで共有のユーザーとしてインスタンス変数に入れるのも良いかもしれません。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:6465df4
reset_avatar(users(:mentormentaro))に修正いたしました。

複数のテストケースで出てくるのであればsetupブロックで共有のユーザーとしてインスタンス変数に入れるのも良いかもしれません。

users(:mentormentaro)は他のテストにも多数出てくるため、 別のissueでリファクタリングとして実施したほうがよいと思い、上記修正としています。

@komagata
Copy link
Copy Markdown
Member

@sjabcdefin

ayu-0505 さんにテストのご協力を依頼中です。

とってもいいと思います!

現状、@mousu-a さんの環境でしか失敗が報告されていません。

でしたら修正は不要です〜

user が1箇所でしか出てこず、可読性向上のため
@sjabcdefin
Copy link
Copy Markdown
Contributor Author

@komagata

  • rails test test/system/page/tags_test.rb:98 について
    現状、@mousu-a さんの環境でしか失敗が報告されていません。
    そのため、Discordで他の方の環境でも同様の失敗が起こるか確認する予定です。

他の方の環境でも同様の失敗が起こることが確認できましたので、test/system/page/tags_test.rb も修正対象といたします。
よろしくお願いいたします。😌🙏

Copy link
Copy Markdown
Member

@komagata komagata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確認させて頂きました。OKです〜🙆‍♂️

@komagata komagata merged commit 23d9bb0 into main Aug 14, 2025
8 checks passed
@komagata komagata deleted the bug/only-pass-headful branch August 14, 2025 06:44
@github-actions github-actions bot mentioned this pull request Aug 14, 2025
87 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants