繁体   English   中英

Rspec视图因可安装Rails 4的引擎而失败

[英]Rspec views fail with Rails 4 mountable engine

我已经创建了一个名为Foobar的可安装Rails 4引擎的Rails 4应用程序。 该引擎包含用于Items的支架。

在引擎中设置rspec-rails后,我无法通过视图规格。

我收到以下错误:

2) foobar/items/index renders a list of items
   Failure/Error: render
   ActionView::Template::Error:
     undefined method `item_path' for #<#<Class:0x007fe6dae7b3c8>:0x007fe6da9a2ec8>
   # ./app/views/foobar/items/index.html.erb:21:in `block in ___sers_ianneub__ocuments__ode_rails_engine_spec_test_engines_foobar_app_views_foobar_items_index_html_erb__1719273292435665376_70314743713380'
   # ./app/views/foobar/items/index.html.erb:16:in `each'
   # ./app/views/foobar/items/index.html.erb:16:in `___sers_ianneub__ocuments__ode_rails_engine_spec_test_engines_foobar_app_views_foobar_items_index_html_erb__1719273292435665376_70314743713380'
   # ./spec/views/foobar/items/index.html.erb_spec.rb:20:in `block (2 levels) in <top (required)>'

在外部应用程序或引擎的规范/虚拟应用程序中启动Rails服务器时,所有视图似乎都可以正常工作。

所以我的问题是:

即使引擎视图在Rails服务器上运行正常,为什么rspec测试仍然失败?

奖励问题:

我可以在index.html.erb中进行哪些更改以使测试通过?

我真的想了解rspec测试中的作用。 恕我直言,rspec(和/或rspec-rails)似乎有一个问题,阻止了此工作。 就像测试中没有加载某些东西一样,这些东西通常会使它在rails服务器中工作。 换句话说:如果它在rails服务器中工作,那么它应该在rspec中以相同的方式工作。 正确吗? 我想念什么?

该应用程序位于此处: https : //github.com/ianneub/rails-engine-spec-test

您应该能够克隆存储库并运行以下命令来复制错误:

  1. cd engines/foobar
  2. bundle install
  3. rspec

预先感谢您与我(以及全世界)分享的任何帮助和指导。

“ index.html.erb”视图中的第21行需要范围确定为foobar gem。

<td><%= link_to 'Show', foobar.item_path(item) %></td>

这是因为Rspec渲染器不包含url_helpers方法,而rails渲染器以某种方式包括了这些方法。 可以用前钩固定:

RSpec.configure do |config|
  config.before(:example, type: :view) do
    view.class_eval do
      include <Your Engine Namespace>::Engine.routes.url_helpers
    end
  end
end

暂无
暂无

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

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