Pregunta

I've created a Rails 4 app with a mountable Rails 4 engine called Foobar. That engine contains a scaffold for Items.

After setting up rspec-rails in the engine, I am unable to get the view specs to pass.

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.

So my question is:

Why do the rspec tests fail, even though the engine views work fine from the rails server?

Bonus question:

What can I change in the index.html.erb to make the test pass?

I'm really trying to understand what is at play here in the rspec tests. IMHO it seems like there is an issue with rspec (and/or rspec-rails) that prevents this from working. Like something isn't being loaded in the tests that would normally make this work inside the rails server. In other words: if it works in the rails server, then it should work in rspec the same way. Would that be correct? What am I missing?

The app is available here: 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).

¿Fue útil?

Solución

Line 21 in the 'index.html.erb' view needs scoping to the foobar gem.

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

Otros consejos

That's because Rspec renderer do not include url_helpers methods, but rails renderer somehow included these methods. 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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top