【问题标题】:haml rspec missing templatehaml rspec 缺少模板
【发布时间】:2012-07-27 20:21:17
【问题描述】:

这真的让我措手不及。安装 haml-rails gem 后,stocks/index.html.haml 在浏览器中呈现没有问题。但是,当我尝试使用 rspec/capybara 进行测试时

describe "StockPages" do
  describe "stocks/index.html.haml" do
    before {visit stocks_path}
    subject {page}
    it { should have_selector('table#Result')  }
  end
end

我收到了这个错误:

Failure/Error: before {visit stocks_path}
     ActionView::MissingTemplate:
       Missing template stocks/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.

我该如何解决这个问题? 谢谢

【问题讨论】:

  • 您是如何将haml-rails gem 添加到您的Gemfile 中的,它只在开发组中吗?
  • 我多么愚蠢!你说的对。所以我也应该将它包含在测试组中?生产怎么样?

标签: ruby-on-rails rspec haml


【解决方案1】:

为了呈现 Haml 模板,您需要加载 haml gem。 haml-rails 确实调用了require 'haml',但如果您只将haml-rails 添加到Gemfile 中的开发组,那么haml 也只会在开发中加载。

为了解决这个问题,您需要将haml-rails 移出任何组以便它始终被加载(因此haml 也总是被加载),或者将gem 'haml' 添加到您的Gemfile(任何组之外)。第一个选项(将 haml-rails 移出开发组)可能是最简单的,但您可能希望将其留在开发组中并显式添加 gem 'haml' 以避免在生产中加载不需要的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    相关资源
    最近更新 更多