3
votes

I have a Rails app which mounts another engine to its routes, and also overrides some routes from the engine. Here is the routes.rb:

Spree::Core::Engine.routes.draw do
  # some other routes
  root :to => "home#index"
end

MyNamespace::Application.routes.draw do
  class CityConstraint
    def matches?(request)
      Spree::CityZone.where(:url => request.params[:city_name]).exists?
    end
  end
  mount Spree::Core::Engine, :at => ':city_name/', :constraints => CityConstraint.new, :as => :city
  mount Spree::Core::Engine, :at => '/'
end

When I try to test the routes with RSpec (2.14), I always get the following error:

#encoding: utf-8
require 'spec_helper'

RSpec.describe "routes.rb" do
  it "test routing" do
    expect(get: "/").to route_to(controller: "spree/home", action: "index")
  end
end
 Failure/Error: expect(get: "/").to route_to(controller: "home", action: "index")
   No route matches "/"
 # ./spec/routing/routes_spec.rb:6:in `block (2 levels) in <top (required)>'

I've found out, that when I add the following line, it works:

RSpec.describe "routes.rb" do
  routes { Spree::Core::Engine.routes } # this sets the routes
  it "test routing" do
    expect(get: "/").to route_to(controller: "spree/home", action: "index")
  end
end

The problem is, that I want to test the whole app, because we mount the app twice, under a city name scope (e.g. /your_city) and under root /.

When I try to set routes { MyNamespace::Application.routes } in my tests, I get the No route matches "/" error.

Any ideas how to test the whole stack of the mounted routes, including the routes from the engine?

1

1 Answers

1
votes

You could try to add the needed routes manually:

RSpec.describe "routes.rb" do
  before :all do
    engine_routes = Proc.new do
      mount Spree::Core::Engine, 
            :at => ':city_name/', 
            :constraints => CityConstraint.new, 
            :as => :city
      mount Spree::Core::Engine, :at => '/'
    end
    Rails.application.routes.send :eval_block, engine_routes
  end

  it "test routing" do
    expect(get: "/").to route_to(controller: "spree/home", action: "index")
  end
end

Idea taken from: http://makandracards.com/makandra/18761-rails-3-4-how-to-add-routes-for-specs-only