4

I'm building a Rails engine under Rails 3.0.12, but I'm having issues with routes when trying to write specs for my engine's controller.

The context

I've been following an Enginex layout. The engine is named Featuring and is not isolated. It does not declare routes by itself: there is no featuring/config/routes.rb file. Instead, a routes_for_feature method is provided for the main application to define engine-specific routes.

##
# featuring/lib/featuring/rails.rb
#
require 'featuring/rails/routing'

module Featuring
  class Engine < ::Rails::Engine
  end
end

##
# featuring/lib/featuring/rails/routing.rb
#
module ActionDispatch::Routing
  class Mapper

    def routes_for_feature(feature_name)
      resource_name = feature_name.to_s.pluralize.to_sym
      resources resource_name, :controller => "featuring/features", :only => [:index, :show], :feature => feature_name.to_s
    end
  end
end

Following the Enginex template, I have a Dummy app which define the routes as so:

# featuring/spec/dummy/config/routes.rb
Dummy::Application.routes.draw do
  routes_for_feature :feature_model
end

The issue

Everything is working fine when I run the rails server for the Dummy app. I can browse to http://localhost:3000/feature_models and the request is successful.

I would like to spec my Featuring::FeaturesController, but I can't get it to find the routes.

Here is the spec:

# featuring/spec/controllers/features_controller_spec.rb
require 'spec_helper'

describe Featuring::FeaturesController do

  context "feature_models" do
    it "GET index should be successful" do
      puts Rails.application.routes.routes
      get :index, { :use_route => "featuring", :feature => "feature_models" }
      response.should be_success
    end
  end
end

And here is the result of running this spec:

rspec spec/controllers/features_controller_spec.rb:7
Featuring::FeaturesController
  feature_models
GET    /feature_models(.:format)                {:action=>"index", :controller=>"featuring/features"}
GET    /feature_models/:id(.:format)            {:action=>"show", :controller=>"featuring/features"}
    GET index should be successful (FAILED - 1)

Failures:

  1) Featuring::FeaturesController feature_models GET index should be successful
     Failure/Error: get :index, { :use_route => "featuring", :feature => "feature_models" }
     ActionController::RoutingError:
       No route matches {:feature=>"feature_models", :controller=>"featuring/features"}
     # ./spec/controllers/features_controller_spec.rb:8:in `block (3 levels) in <top (required)>'

As you can see, even if the routes are correctly defined, the spec'ed controller seems not to find them.

Something surprises me in the RoutingError: No route matches {:feature=>"feature_models", :controller=>"featuring/features"}. The action => "index" is not displayed.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Romain Champourlier
  • 2,360
  • 24
  • 29
  • I'm having this issue as well in rails 3.2.3. I know for sure that it's not related to :action => "index", but I'm still stumped as to why I'm getting this RoutingError... – Amiel Martin Sep 25 '12 at 20:12

2 Answers2

0

I had a similar error, and I was also confused by the lack of {:action => "index"} in the route options. However, this turned out not to be the problem. ActionDispatch treats the lack of an :action as equivalent to {:action => "index"}. See:

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/route_set.rb#L545

You may be missing a request parameter in your spec, as was the case with me. Check the Parameters line in your server log when you load the page in the browser.

0

The trick is to add routes { } to your spec, like so:

describe Featuring::FeaturesController do
  routes { Featuring::Engine.routes }
  # ...
end

See also No Route Matches ... Rails Engine

Community
  • 1
  • 1
berkes
  • 26,996
  • 27
  • 115
  • 206