Rspec-rails: Routing error in the mountable rails plugin application.

Created on 9 Feb 2015  路  18Comments  路  Source: rspec/rspec-rails

In command line

rails plugin new ENGINE_NAME --dummy-path=spec/dummy --skip-test-unit --mountable

gemspec file

s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'factory_girl_rails'

rails_helper.rb

require File.expand_path('../dummy/config/environment', __FILE__)

engine.rb

 config.generators do |g|
      g.test_framework      :rspec,        :fixture => false
      g.fixture_replacement :factory_girl, :dir => 'spec/factories'
end

Command line

rails g scaffold NAME name:string

And run 'rspec' in command

  1) Backend::ShopsController GET new assigns a new shop as @shop
     Failure/Error: get :new, {}, valid_session
     ActionController::UrlGenerationError:
       No route matches {:action=>"new", :controller=>"backend/shops"}
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/journey/formatter.rb:46:in `generate'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:710:in `generate'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:741:in `generate'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:736:in `generate_extras'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:731:in `extra_keys'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_controller/test_case.rb:205:in `assign_parameters'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_controller/test_case.rb:616:in `process'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_controller/test_case.rb:65:in `process'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.0/lib/action_controller/test_case.rb:505:in `get'
     # ./spec/controllers/backend/shops_controller_spec.rb:58:in `block (3 levels) in <module:Backend>'

  2) Backend::ShopsController routing routes to #index
     Failure/Error: expect(:get => "/shops").to route_to("shops#index")
       No route matches "/shops"
     # ./spec/routing/backend/shops_routing_spec.rb:8:in `block (3 levels) in <module:Backend>'

  3) Backend::ShopsController routing routes to #new
     Failure/Error: expect(:get => "/shops/new").to route_to("shops#new")
       No route matches "/shops/new"
     # ./spec/routing/backend/shops_routing_spec.rb:12:in `block (3 levels) in <module:Backend>'

  4) Backend::ShopsController routing routes to #show
     Failure/Error: expect(:get => "/shops/1").to route_to("shops#show", :id => "1")
       No route matches "/shops/1"
     # ./spec/routing/backend/shops_routing_spec.rb:16:in `block (3 levels) in <module:Backend>'

  5) Backend::ShopsController routing routes to #edit
     Failure/Error: expect(:get => "/shops/1/edit").to route_to("shops#edit", :id => "1")
       No route matches "/shops/1/edit"
     # ./spec/routing/backend/shops_routing_spec.rb:20:in `block (3 levels) in <module:Backend>'

  6) Backend::ShopsController routing routes to #create
     Failure/Error: expect(:post => "/shops").to route_to("shops#create")
       No route matches "/shops"
     # ./spec/routing/backend/shops_routing_spec.rb:24:in `block (3 levels) in <module:Backend>'

  7) Backend::ShopsController routing routes to #update
     Failure/Error: expect(:put => "/shops/1").to route_to("shops#update", :id => "1")
       No route matches "/shops/1"
     # ./spec/routing/backend/shops_routing_spec.rb:28:in `block (3 levels) in <module:Backend>'

  8) Backend::ShopsController routing routes to #destroy
     Failure/Error: expect(:delete => "/shops/1").to route_to("shops#destroy", :id => "1")
       No route matches "/shops/1"
     # ./spec/routing/backend/shops_routing_spec.rb:32:in `block (3 levels) in <module:Backend>'

  9) shops/edit renders the edit shop form
     Failure/Error: @shop = assign(:shop, Shop.create!(
     NameError:
       uninitialized constant Shop
     # ./spec/views/backend/shops/edit.html.erb_spec.rb:5:in `block (2 levels) in <top (required)>'

  10) shops/index renders a list of shops
     Failure/Error: Shop.create!(
     NameError:
       uninitialized constant Shop
     # ./spec/views/backend/shops/index.html.erb_spec.rb:6:in `block (2 levels) in <top (required)>'

  11) shops/new renders new shop form
     Failure/Error: assign(:shop, Shop.new(
     NameError:
       uninitialized constant Shop
     # ./spec/views/backend/shops/new.html.erb_spec.rb:5:in `block (2 levels) in <top (required)>'

  12) shops/show renders attributes in <p>
     Failure/Error: @shop = assign(:shop, Shop.create!(
     NameError:
       uninitialized constant Shop
     # ./spec/views/backend/shops/show.html.erb_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.02859 seconds (files took 1.01 seconds to load)
29 examples, 12 failures, 17 pending

Failed examples:

rspec ./spec/controllers/backend/shops_controller_spec.rb:57 # Backend::ShopsController GET new assigns a new shop as @shop
rspec ./spec/routing/backend/shops_routing_spec.rb:7 # Backend::ShopsController routing routes to #index
rspec ./spec/routing/backend/shops_routing_spec.rb:11 # Backend::ShopsController routing routes to #new
rspec ./spec/routing/backend/shops_routing_spec.rb:15 # Backend::ShopsController routing routes to #show
rspec ./spec/routing/backend/shops_routing_spec.rb:19 # Backend::ShopsController routing routes to #edit
rspec ./spec/routing/backend/shops_routing_spec.rb:23 # Backend::ShopsController routing routes to #create
rspec ./spec/routing/backend/shops_routing_spec.rb:27 # Backend::ShopsController routing routes to #update
rspec ./spec/routing/backend/shops_routing_spec.rb:31 # Backend::ShopsController routing routes to #destroy
rspec ./spec/views/backend/shops/edit.html.erb_spec.rb:10 # shops/edit renders the edit shop form
rspec ./spec/views/backend/shops/index.html.erb_spec.rb:15 # shops/index renders a list of shops
rspec ./spec/views/backend/shops/new.html.erb_spec.rb:10 # shops/new renders new shop form
rspec ./spec/views/backend/shops/show.html.erb_spec.rb:10 # shops/show renders attributes in <p>

How can I fix this?

Engine

All 18 comments

Add routes { MyEngine::Engine.routes } to your controller spec. As a side note, github issues are not the place to request support, please use the mailing list (https://groups.google.com/forum/#!forum/rspec) or #rspec on freenode.

I think this is the Rspec code generator bug on the Rails mountable plugin project.
After add routes { MyEngine::Engine.routes } on my controller spec.
But the below problem was not solved.

1) shops/edit renders the edit shop form
Failure/Error: @shop = assign(:shop, stub_model(Shop,
NameError:
uninitialized constant Shop
# ./spec/views/backend/shops/edit.html.erb_spec.rb:5:in `block (2 levels) in

2) shops/index renders a list of shops
Failure/Error: Shop.create!(
NameError:
uninitialized constant Shop
# ./spec/views/backend/shops/index.html.erb_spec.rb:6:in `block (2 levels) in

3) shops/new renders new shop form
Failure/Error: assign(:shop, Shop.new(
NameError:
uninitialized constant Shop
# ./spec/views/backend/shops/new.html.erb_spec.rb:5:in `block (2 levels) in

4) shops/show renders attributes in


Failure/Error: @shop = assign(:shop, Shop.create!(
NameError:
uninitialized constant Shop
# ./spec/views/backend/shops/show.html.erb_spec.rb:5:in `block (2 levels) in

Finished in 0.06133 seconds (files took 1.16 seconds to load)
29 examples, 4 failures, 17 pending

Well shop is probably MyEngine::Shop ?

Yes, MyEngine::Shop.
It was generated from the command "rails g scaffold shop".

module Backend
  class Shop < ActiveRecord::Base
  end
end

Generated rspec code.

require 'rails_helper'

RSpec.describe "shops/index", :type => :view do
  before(:each) do
    assign(:shops, [
      Shop.create!(
        :name => "Name"
      ),
      Shop.create!(
        :name => "Name"
      )
    ])
  end

  it "renders a list of shops" do
    render
    assert_select "tr>td", :text => "Name".to_s, :count => 2
  end
end

I've gotten some other errors in the process of using engines. I plan on getting to this and several other issues, between now and Monday.

@joowh85 yep, so you need to reference it as Backend::Shop thats how ruby works.

Yep, But I can't found related articles, documents, any other.
Do you have a some links for that?
And Thanks guys (:

@joowh85 Unfortunately we do not yet. This is my main goal right now per #1227. We've had a sudden influx of users of engines recently. So I'm treating this as a top priority for myself right now.

@joowh85 please note that the constant lookup stuff is nothing to do with the engine and is purely down to how Ruby modules work, try this for a guide to how they work: http://rubymonk.com/learning/books/1-ruby-primer/chapters/35-modules/lessons/80-modules-as-namespaces

Thanks @JonRowe

Uninitialized constant was solved after adding the "Engine::Shop" but other issue pops up.

require 'rails_helper'

RSpec.describe "shops/edit", :type => :view do
  before(:each) do
    @shop = assign(:shop, Backend::Shop.create!(
      :name => "MyString"
    ))
  end

  it "renders the edit shop form" do
    render

    assert_select "form[action=?][method=?]", shop_path(@shop), "post" do

      assert_select "input#shop_name[name=?]", "shop[name]"
    end
  end
end

Stack traces.

  1) shops/edit renders the edit shop form
     Failure/Error: render
     ActionView::MissingTemplate:
       Missing template shops/edit with {:locale=>[:ko], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby]}. Searched in:
         * "/home/sangwoojoo/work/backend/spec/dummy/app/views"
         * "/home/sangwoojoo/work/backend/app/views"
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionview-4.2.0/lib/action_view/path_set.rb:46:in `find'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionview-4.2.0/lib/action_view/lookup_context.rb:121:in `find'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionview-4.2.0/lib/action_view/renderer/abstract_renderer.rb:18:in `find_template'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionview-4.2.0/lib/action_view/renderer/template_renderer.rb:40:in `determine_template'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionview-4.2.0/lib/action_view/renderer/template_renderer.rb:8:in `render'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionview-4.2.0/lib/action_view/renderer/renderer.rb:42:in `render_template'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionview-4.2.0/lib/action_view/renderer/renderer.rb:23:in `render'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionview-4.2.0/lib/action_view/helpers/rendering_helper.rb:32:in `render'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionview-4.2.0/lib/action_view/test_case.rb:199:in `render'
     # /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/actionview-4.2.0/lib/action_view/test_case.rb:120:in `render'
     # ./spec/views/backend/shops/edit.html.erb_spec.rb:11:in `block (2 levels) in <top (required)>'

Finished in 0.01827 seconds (files took 1.04 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/views/backend/shops/edit.html.erb_spec.rb:10 # shops/edit renders the edit shop form
routes { MyEngine::Engine.routes } <- this also not works.


/home/sangwoojoo/work/backend/spec/views/backend/shops/index.html.erb_spec.rb:4:in `block in <top (required)>': undefined method `routes' for RSpec::ExampleGroups::ShopsIndex:Class (NoMethodError)
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `module_exec'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `subclass'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:219:in `block in define_example_group_method'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/dsl.rb:41:in `block in expose_example_group_alias'
    from /home/sangwoojoo/work/backend/spec/views/backend/shops/index.html.erb_spec.rb:3:in `<top (required)>'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `load'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/bin/rspec:23:in `<main>'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
    from /home/sangwoojoo/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'

These seen to stem from the fact you are loading the dummy app as the main rails source. This means all of the default controller and view lookups, and auto loading, come from it; not necessarily the engine. Make sure you have mounted your engine accordingly in it. I am aware of this common problem and is something I am working to address.

I麓ve got the same error using routes { MyEngine::Engine.routes }: ActionController::UrlGenerationError: No route matches

The old way { use_route: ... } passed to my get or post method works. Any idea?

The old way { use_route: ... } passed to my get or post method works

@andyw8 What way is this? That is not part of rspec-rails.

@cupakromer I think you replied to the wrong person.

@cupakromer I麓m passing the route to my get method get :index, { use_route: MyEngine }

looks like use_route doesn't work with rails 5.1.4 rspec-rails 3.6.1. anyone know how to bypass this problem in the current versions of rails/rspec-rails?

@take I found this, it seems to be removed.

Was this page helpful?
0 / 5 - 0 ratings