使用引擎路由

路由规范可以指定将用于示例组的路由集。这在测试 Rails 引擎时最有用。

指定引擎路由

假设有一个名为“spec/routing/engineroutesspec.rb”的文件,其中包含

require "rails_helper"

# A very simple Rails engine
module MyEngine
  class Engine < ::Rails::Engine
    isolate_namespace MyEngine
  end

  Engine.routes.draw do
    resources :widgets, :only => [:index]
  end

  class WidgetsController < ::ActionController::Base
    def index
    end
  end
end

RSpec.describe MyEngine::WidgetsController, type: :routing do
  routes { MyEngine::Engine.routes }

  it "routes to the list of all widgets" do
    expect(:get => widgets_path).
      to route_to(:controller => "my_engine/widgets", :action => "index")
  end
end

我运行 rspec spec

那么这些示例应该全部通过。