mardi 13 octobre 2015

RSpec test routes with mounted engine

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?

Aucun commentaire:

Enregistrer un commentaire