简体   繁体   English

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

[英]Rspec views fail with Rails 4 mountable engine

I've created a Rails 4 app with a mountable Rails 4 engine called Foobar . 我已经创建了一个名为Foobar的可安装Rails 4引擎的Rails 4应用程序。 That engine contains a scaffold for Items . 该引擎包含用于Items的支架。

After setting up rspec-rails in the engine, I am unable to get the view specs to pass. 在引擎中设置rspec-rails后,我无法通过视图规格。

I get the following error: 我收到以下错误:

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)>'

When starting up the rails server in either the outer application or the engine's spec/dummy app the views all seem to work just fine. 在外部应用程序或引擎的规范/虚拟应用程序中启动Rails服务器时,所有视图似乎都可以正常工作。

So my question is: 所以我的问题是:

Why do the rspec tests fail, even though the engine views work fine from the rails server? 即使引擎视图在Rails服务器上运行正常,为什么rspec测试仍然失败?

Bonus question: 奖励问题:

What can I change in the index.html.erb to make the test pass? 我可以在index.html.erb中进行哪些更改以使测试通过?

I'm really trying to understand what is at play here in the rspec tests. 我真的想了解rspec测试中的作用。 IMHO it seems like there is an issue with rspec (and/or rspec-rails) that prevents this from working. 恕我直言,rspec(和/或rspec-rails)似乎有一个问题,阻止了此工作。 Like something isn't being loaded in the tests that would normally make this work inside the rails server. 就像测试中没有加载某些东西一样,这些东西通常会使它在rails服务器中工作。 In other words: if it works in the rails server, then it should work in rspec the same way. 换句话说:如果它在rails服务器中工作,那么它应该在rspec中以相同的方式工作。 Would that be correct? 正确吗? What am I missing? 我想念什么?

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

You should be able to clone the repo and run these commands to duplicate the error: 您应该能够克隆存储库并运行以下命令来复制错误:

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

Thank you in advance for any help and guidance that you share with me (and the world). 预先感谢您与我(以及全世界)分享的任何帮助和指导。

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

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

That's because Rspec renderer do not include url_helpers methods, but rails renderer somehow included these methods. 这是因为Rspec渲染器不包含url_helpers方法,而rails渲染器以某种方式包括了这些方法。 Could be fixed by a before hook: 可以用前钩固定:

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