简体   繁体   English

升级到 Rails 6 后,rspec-rails 在 controller 测试中停止存根视图

[英]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.将我的 Rails 应用程序升级到版本 6 后,rspec 停止存根视图渲染。 ( https://relishapp.com/rspec/rspec-rails/v/5-0/docs/controller-specs/views-are-stubbed-by-default ) ( 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.我已经尝试了几个 rspec 和 rspec-rails 版本,但结果总是一样的。

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.预期的行为是调用 render 应该产生一个空字符串,但在规范中调用 controller 操作会像正常情况一样呈现模板。

Calling render_views false in the example group has not effect.在示例组中调用render_views false没有效果。 render_views? returns false in the example.在示例中返回false

When I debug the controller action with binding.pry , in Rails 5, calling render returns "" .当我使用binding.pry调试 controller 操作时,在 Rails 5 中,调用render返回"" In Rails 6, calling render renders the real template.在 Rails 6 中,调用render渲染真正的模板。

the example is as simple as it can be:该示例尽可能简单:

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

Completing the loop here, OP figured out this within a GitHub issue:在这里完成循环,OP 在 GitHub 问题中解决了这个问题:

https://github.com/rspec/rspec-rails/issues/2517 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.这是由于在before_action回调中设置view_paths并使用此新路径在 controller 上设置布局引起的。显然在以前的 Rails 版本中这是可行的,但现在有必要在 class 级别(使用prepend_view_paths或类似的)设置view_paths以存根工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM