All routing examples do not work for Rails 3.2 Engine with RSpec 2.10

I created the Rails Engine in normal mode , installed RSpec and created a scaffold for the model, but I cannot get any routing specifications.

Here is an example:

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

I run the examples in a dummy application as follows:

 $ 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 a fictitious application:

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

And I can go into the dummy application and start the console and get this route simply:

 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 is some mismatch between the dummy application and RSpec, and I cannot understand what it is. I found several articles that claim to solve this problem, but none of them helped, and some of them relate to Rails 3.1:

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

+8
rspec-rails rspec2 rails-engines
source share
2 answers

Update (2013-10-31)

RSpec 2.14 now fully supports machine routes :

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

Add it to all routing specifications:

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

The best solution I could find explicitly defines the set of routes 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 

It seems a bit hacky, but it makes sense. I do not check if Rails can mount the engine in a specific way. I am only testing that my engine correctly configured its own routes. However, I would prefer not to rewrite the instance variable for this to happen.

Here are a couple of good topics on this topic:

  • How to test routes in a Rails 3.1 mounted engine
  • How to write a Rails 3.1 engine controller test in rspec?
+10
source share

I think you need to explicitly tell RSpec that you are using a Rails route with names. EG,

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

Also, why not simplify the writing of RSpec controller tests, for example,

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

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

+7
source share

All Articles