3
votes

In my engine 'Utilizer', I'm trying to test routes with Rspec 3.2. Namespace are isolated

# lib/utilizer/engine.rb
module Utilizer
    class Engine < ::Rails::Engine
        isolate_namespace Utilizer
        ...
    end
end

The engine is mounted to the dummy app:

    # spec/dummy/config/routes.rb
    Rails.application.routes.draw do
      mount Utilizer::Engine => "/utilizer", as: 'utilizer'
    end

To the spec_helper.rb I've added a couple of configures as below (from here):

# spec/spec_helper.rb
RSpec.configure do |config|
  ...
  config.before(:each, type: :routing)    { @routes = Utilizer::Engine.routes }
  ...
end 

When I defined a route:

# config/routes.rb
Utilizer::Engine.routes.draw do
  resources :auths, id: /\d+/, only: [:destroy]
end

Rake shows it properly for the dummy app:

$ spec/dummy > bundle exec rake routes

$ utilizer /utilizer Utilizer::Engine
$ Routes for Utilizer::Engine:
$ auth DELETE /auths/:id(.:format) utilizer/auths#destroy {:id=>/\d+/}

BUT both Rspec tests

# spec/routing/auths_routing_spec.rb
require 'spec_helper'

describe "Auths page routing" do

  let!(:auth) { create(:utilizer_auth) } # factory is working properly by itself

  describe "#destroy" do
    let!(:action) { { controller: "utilizer/auths", action: "destroy", id: auth.id } }
    specify { { delete: "/auths/#{ auth.id }" }.should route_to(action) }
    specify { { delete: auth_path(auth) }.should route_to(action) }
  end
end

fail with errors (for the first and second tests correspondingly):

No route matches "/auths/1"
No route matches "/utilizer/auths/1"

But, Holmes, why?

2

2 Answers

8
votes

Since RSpec 2.14 you can use the following:

describe "Auths page routing" do
  routes { Utilizer::Engine.routes }
  # ...
end

Source: https://github.com/rspec/rspec-rails/pull/668

0
votes

I've fount a solution in Exoth's comment at the end of this discussion on Github (thanks to Brandan).

In my spec_helper instead of

config.before(:each, type: :routing) { @routes = Utilizer::Engine.routes }

I use

config.before(:each, type: :routing) do
  @routes = Utilizer::Engine.routes
  assertion_instance.instance_variable_set(:@routes, Utilizer::Engine.routes)
end

and it works.