使用 HAML 而不是 ERB 导致测试失败

Using HAML instead of ERB causes tests to fail

这看起来很基础,但我使用的是 Rails 5 并且我将所有视图交换为 HAML 而不是 ERB。现在我的一项测试失败了:

ActionController::UnknownFormat: ProductsController#index is missing  a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []
    test/controllers/products_controller_test.rb:9:in `block in <class:ProductsControllerTest>'

是否有任何地方可以更改默认值,或者是否有其他问题我必须解决?我的路线很好,我可以毫无问题地访问索引页面。

您似乎在 app/views/products

中缺少视图文件 index.html.haml

您需要安装 haml,将 gem 'haml' 添加到 Gemfile,下一步 bundle install 在您的控制台中。

这意味着控制器中缺少 #index 方法的模板。 Rails 有一个约定,它将在 /views/ 目录中搜索此文件,相关的 html.erb 或 haml 文件,遵循名称约定。

我看到了同样的问题,我们的解决方案是将 haml gem 添加到 Gemfiledevelopment 组中,确保 haml 相关 gem 不在任何组中