rails 4可安装发动机的RSPEC视图失败



我已经创建了一个带有可安装的导轨4引擎Foobar的Rails 4应用程序。该引擎包含Items的脚手架。

在引擎中设置RSPEC轨道后,我无法获得视图规格。

我有以下错误:

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 Server时,视图都可以正常工作。

所以我的问题是:

为什么RSPEC测试即使引擎视图从Rails Server正常工作?

奖金问题:

我可以在index.html.erb中更改如何进行测试?

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

该应用在此处可用:https://github.com/ianneub/rails-engine-pec-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

最新更新