mcpField
I
inertia-rails-testing

inertia-rails-testing

github.com/inertia-railsテスト・品質70467 回インストール37 週間前に更新
リポジトリ

inertia-rails-testing について

Rails アプリケーションの Inertia レスポンスを、RSpec または Minitest でテストするためのガイドです。レスポンスマッチャー、プロパティ、フラッシュメッセージ、遅延プロパティ、リダイレクト、リクエストテストを扱います。

主な機能

  1. 01RSpec Inertia マッチャー
  2. 02プロパティとフラッシュのアサーション
  3. 03遅延プロパティ用テストヘルパー
  4. 04部分リロード用ヘルパー
  5. 05Minitest リファレンス

活用シーン

  1. 01描画コンポーネントとプロパティの確認
  2. 02リダイレクト後のフラッシュデータのテスト
  3. 03遅延プロパティと再読み込みの検証
  4. 04リクエストスペックでの Inertia エンドポイントのテスト

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/inertia-rails/skills --skill inertia-rails-testing

インストール後、Claude Code で/inertia-rails-testingを入力して読み込まれたか確認します。

同梱ファイルreferences/minitest.md

詳細

Inertia Rails のテスト

RSpec と Minitest を使った Inertia レスポンスのテストパターンです。

各コントローラーアクションで、次の点を確認します。

  • 正しいコンポーネント → render_component('users/index')
  • 期待される props → have_props(users: satisfy { ... })
  • データが漏洩していないこと → have_no_prop(:secret)
  • フラッシュメッセージ → follow_redirect! の後に have_flash(notice: '...')
  • 遅延 props → have_deferred_props(:analytics)

よくある間違い: PRG の後に follow_redirect! を忘れることです。これを行わないと、後続の Inertia ページではなく、302 リダイレクトレスポンスを検証することになります。

セットアップ

# spec/rails_helper.rb
require 'inertia_rails/rspec'

RSpec マッチャー

マッチャー用途
be_inertia_responseレスポンスが Inertia 形式であることを確認します
render_component('path')レンダリングされたコンポーネント名を確認します
have_props(key: value)props の一部が一致することを確認します
have_exact_props(key: value)props が完全に一致することを確認します
have_no_prop(:key)prop が存在しないことを確認します
have_flash(key: value)flash の一部が一致することを確認します
have_exact_flash(key: value)flash が完全に一致することを確認します
have_no_flash(:key)flash が存在しないことを確認します
have_deferred_props(:key)遅延 props が存在することを確認します
have_view_data(key: value)view_data の一部が一致することを確認します

RSpec の例

必ずマッチャー(render_component、have_props、have_flash)を使い、 プロパティへの直接アクセス(inertia.component、inertia.props[:key])は使わないでください。

# BAD — direct property access:
# expect(inertia.component).to eq('users/index')
# expect(inertia.props[:users].length).to eq(3)

# GOOD — use matchers:
expect(inertia).to render_component('users/index')
expect(inertia).to have_props(users: satisfy { |u| u.length == 3 })
# Key pattern: follow_redirect! after POST/PATCH/DELETE before asserting
it 'redirects with flash on success' do
  post users_path, params: { user: valid_params }

  expect(response).to redirect_to(users_path)
  follow_redirect!
  expect(inertia).to have_flash(notice: 'User created!')
end

it 'returns validation errors on failure' do
  post users_path, params: { user: { name: '' } }

  follow_redirect!
  expect(inertia).to have_props(errors: hash_including('name' => anything))
end
共有 props のテスト

inertia_share の共有 props は inertia.props に含まれます。inertia_rails/rspec を読み込むと、type: :request のスペックで inertia ヘルパーを使用できます。

it 'includes shared auth data' do
  sign_in(user)
  get dashboard_path

  expect(inertia).to have_props(
    auth: hash_including(user: hash_including(id: user.id))
  )
end
it 'excludes auth data for unauthenticated users' do
  get dashboard_path

  expect(inertia).to have_props(auth: hash_including(user: nil))
end
遅延 props のテスト
it 'defers expensive analytics data' do
  get dashboard_path

  expect(inertia).to have_deferred_props(:analytics, :statistics)
  expect(inertia).to have_deferred_props(:slow_data, group: :slow)
end
部分リロードのヘルパー
it 'supports partial reload' do
  get users_path

  # Simulate partial reload — only fetch specific props
  inertia_reload_only(:users, :pagination)

  # Or exclude specific props
  inertia_reload_except(:expensive_stats)

  # Load deferred props
  inertia_load_deferred_props(:default)
  inertia_load_deferred_props # loads all groups
end
外部リダイレクトのテスト(inertia_location)
it 'redirects to Stripe via inertia_location' do
  post checkout_path

  # inertia_location returns 409 with X-Inertia-Location header
  expect(response).to have_http_status(:conflict)
  expect(response.headers['X-Inertia-Location']).to match(/stripe\.com/)
end
テストしてはいけない項目
  • Inertia フレームワークの動作 — <Form> が CSRF トークンを送信することや、router.visit が動作することはテストしないでください。テストするのは、あなたのコントローラーロジックです。
  • prop の正確な構造 — 完全な JSON との深い等価性ではなく、have_props(users: satisfy { ... }) を使ってください。脆弱なテストは、列を追加したときに失敗します。
  • follow_redirect! を使わない flash のテスト — リダイレクトを伴う POST/PATCH/DELETE の後は、flash や props を検証する前に必ず follow_redirect! を実行してください。実行しないと、ページではなく 302 を検証することになります。
  • 初回読み込み時の遅延 prop の値 — 遅延 props は、ページのレンダリング後に別のリクエストで取得されるため、初回レスポンスでは nil です。登録されていることを確認するには have_deferred_props(:key) を使い、テスト内で解決するには inertia_load_deferred_props を使ってください。
  • モックした Inertia レスポンス — スタック全体を実行する type: :request のスペックを使ってください。assigns を使う type: :controller のスペックは、Inertia がフルリクエストサイクルでのみ実行されるカスタムのレンダリングパイプラインを使用するため、動作しません。
直接アクセス(可能な場合は上記のマッチャーを使用してください)
inertia.component       # => 'users/index'
inertia.props           # => { users: [...] }
inertia.props[:users]   # direct prop access
inertia.flash           # => { notice: 'Created!' }
inertia.deferred_props  # => { default: [:analytics], slow: [:report] }

関連スキル

  • コントローラーパターン → inertia-rails-controllers(prop の型、flash、PRG)
  • フォームのフロー → inertia-rails-forms(送信、バリデーション)
  • 遅延/共有 props → inertia-rails-pages(Deferred、usePage)

参考資料

必須 — Minitest のテストを書く場合(RSpec ではない場合)は、ファイル全体を読んでください: references/minitest.md(約40行)— 上記の RSpec マッチャーに相当する Minitest のアサーションです。

RSpec のプロジェクトでは minitest.md を読み込まないでください。上記のマッチャーだけで十分です。

コメント