Skip to content

Fix ViewRendering after hook wiping controller view_paths when controller is nil in before hook#2897

Open
krtierney wants to merge 1 commit into
rspec:mainfrom
krtierney:fix/view-rendering-nil-controller-view-paths-corruption
Open

Fix ViewRendering after hook wiping controller view_paths when controller is nil in before hook#2897
krtierney wants to merge 1 commit into
rspec:mainfrom
krtierney:fix/view-rendering-nil-controller-view-paths-corruption

Conversation

@krtierney
Copy link
Copy Markdown

The render_views? instance method returns different values in the before and after hooks when controller changes between them. This happens in request specs where controller is nil before the first request but set to a real controller instance afterwards:

  • before: controller is nil → NilClass doesn't respond_to?(:view_paths) → render_views? returns true → skips saving @_original_path_set
  • after: controller is set → controller.class responds_to?(:view_paths) → render_views? returns false → restores @_original_path_set (nil) → wipes controller.class.view_paths to an empty PathSet

This class-level mutation persists across examples, causing ActionView::MissingTemplate errors in subsequent tests that depend on the same controller class having valid view paths.

The fix adds a nil guard on @_original_path_set in the after hook so the restore only runs when the before hook actually saved the paths.

Fixes #2602

@krtierney krtierney force-pushed the fix/view-rendering-nil-controller-view-paths-corruption branch from c1a18ce to a5a0c89 Compare April 15, 2026 10:30
@krtierney krtierney changed the title Fix ViewRendering after hook wiping controller view_paths when contro ller is nil in before hook Fix ViewRendering after hook wiping controller view_paths when controller is nil in before hook Apr 15, 2026
…ller is nil in before hook

The `render_views?` instance method returns different values in the
`before` and `after` hooks when `controller` changes between them.
This happens in request specs where `controller` is nil before the
first request but set to a real controller instance afterwards:

- `before`: controller is nil → NilClass doesn't respond_to?(:view_paths)
  → render_views? returns true → skips saving @_original_path_set
- `after`: controller is set → controller.class responds_to?(:view_paths)
  → render_views? returns false → restores @_original_path_set (nil)
  → wipes controller.class.view_paths to an empty PathSet

This class-level mutation persists across examples, causing
ActionView::MissingTemplate errors in subsequent tests that depend on
the same controller class having valid view paths.

The fix adds a nil guard on @_original_path_set in the after hook so
the restore only runs when the before hook actually saved the paths.

Fixes rspec#2602
@krtierney krtierney force-pushed the fix/view-rendering-nil-controller-view-paths-corruption branch from a5a0c89 to 6e4aa2a Compare April 15, 2026 10:35
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.

Skipping controller test in a config-based before hook corrupts view paths

1 participant