Brandan
Brandan

Reputation: 14983

All routing examples fail for a Rails 3.2 Engine with RSpec 2.10

I've created a Rails Engine in the normal fashion, installed RSpec, and generated a scaffold for a model, but I can't get any routing specs to pass.

Here's one example:

describe Licensing::LicensesController do
  it 'routes to #index' do
    get('/licensing/licenses').should route_to('licensing/licenses#index')
  end
end

I'm running the examples in the dummy app like this:

$ cd spec/dummy
$ rake spec
/Users/brandan/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec ../routing/licensing/licenses_routing_spec.rb
F

Failures:

  1) Licensing::LicensesController routes to #index
     Failure/Error: get('/licensing/licenses').should route_to('licensing/licenses#index')
       No route matches "/licensing/licenses"
     # /Users/brandan/repos/licensing/spec/routing/licensing/licenses_routing_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.04345 seconds
1 example, 1 failure

The engine is mounted correctly in the dummy app:

# spec/dummy/config/routes.rb
Rails.application.routes.draw do
  mount Licensing::Engine => "/licensing"
end

And I can go into the dummy app and fire up the console and get that route just fine:

1.9.3p194 :001 > app.get('/licensing/licenses')
  Licensing::License Load (0.3ms)  SELECT "licensing_licenses".* FROM "licensing_licenses" 
200
1.9.3p194 :002 > app.response.body
"<!DOCTYPE html>..."

There's some discrepancy between the dummy app and RSpec, and I can't figure out what it is. I've found several articles that claim to solve this problem, but none of them have helped, and several of them are specific to Rails 3.1:

Has anybody solved this problem in Rails 3.2/RSpec 2.10?

Upvotes: 8

Views: 3020

Answers (2)

Brandan
Brandan

Reputation: 14983

Update (2013-10-31)

RSpec 2.14 now fully supports engine routes:

module MyEngine
  describe ExampleController do
    routes { MyEngine::Engine.routes }
  end
end

Add it to all routing specs:

# spec/spec_helper.rb
config.include Module.new {
  def self.included(base)
    base.routes { Reportr::Engine.routes }
  end 
}, type: :routing

The best solution I've been able to find so far is explicitly setting the route set that will be used during the tests:

RSpec.configure do |config|
  config.before(:each, type: :controller) { @routes = Licensing::Engine.routes }
  config.before(:each, type: :routing)    { @routes = Licensing::Engine.routes }
end

Then I had to rewrite my routing examples to omit the namespace:

it 'routes to #index' do
  get('/licenses').should route_to('licensing/licenses#index')
end

This seems like a bit of a hack, but it does make sense. I'm not testing whether Rails is able to mount an engine at a particular path. I'm only testing that my engine has correctly set up its own routes. Still, I'd rather not have to overwrite an instance variable to make it happen.

Here are another couple of good threads on this topic:

Upvotes: 10

westonplatter
westonplatter

Reputation: 1495

I think you need to explicitly inform RSpec that you are using a namespaced Rails route. EG,

# :use_route => ENGINE_NAMESPACE
:use_route => 'licensing'

Additionally, why not simplify the writting of RSpec controller tests like this,

describe Licensing::LicensesController do
  it 'GET index' do
    get :index, use_route: 'licensing'
  end
end

Here's a link for RSpec controllers I did, https://github.com/westonplatter/questionnaire_engine/tree/engine/spec/controllers

Upvotes: 7

Related Questions