Search code examples
ruby-on-railsrubyrspecrspec-railsruby-on-rails-6

rspec-rails stopped stubbing views in controller test after upgrading to Rails 6


After upgrading my Rails application to version 6, rspec stopped stubbing view rendering. (https://relishapp.com/rspec/rspec-rails/v/5-0/docs/controller-specs/views-are-stubbed-by-default)

I have tried several rspec and rspec-rails versions but result is always the same.

The expected behavior is that calling render should result in an empty string, but instead calling a controller action in spec is rendering the template like it would normally.

Calling render_views false in the example group has not effect. render_views? returns false in the example.

When I debug the controller action with binding.pry, in Rails 5, calling render returns "". In Rails 6, calling render renders the real template.

the example is as simple as it can be:

      it "is success" do
        get 'index'
        expect(response).to be_successful
      end

Solution

  • Completing the loop here, OP figured out this within a GitHub issue:

    https://github.com/rspec/rspec-rails/issues/2517

    This was caused by setting view_paths in before_action callback and using this new path to set a layout on the controller. Apparently in previous versions of Rails this worked, but now it's necessary to set view_paths on class level (using prepend_view_paths or similar) for stubbing to work.