2

我用这条路线创建了一个可安装的引擎:

Rails.application.routes.draw do
  scope module: :minimalist_cms do
    root to: "pages#show"
    get ':id', to: "pages#show"
    resources :pages
    resources :page_parts
  end
end

我有这个测试:

require 'spec_helper'

module MinimalistCms
  describe PagesController, type: :controller do

    let(:page) { stub_model(Page) }

    describe :show do
      context "when there is a page" do
        it 'should find by slug' do
          Page.should_receive(:find_by_slug).and_return(page)
          get :show, id: 'test'
        end
      end
    end
  end
end

当我输入时,rake routes我得到了这个结果:

          root        /                              minimalist_cms/pages#show
               GET    /:id(.:format)                 minimalist_cms/pages#show
         pages GET    /pages(.:format)               minimalist_cms/pages#index
               POST   /pages(.:format)               minimalist_cms/pages#create
      new_page GET    /pages/new(.:format)           minimalist_cms/pages#new
     edit_page GET    /pages/:id/edit(.:format)      minimalist_cms/pages#edit
          page GET    /pages/:id(.:format)           minimalist_cms/pages#show
               PUT    /pages/:id(.:format)           minimalist_cms/pages#update
               DELETE /pages/:id(.:format)           minimalist_cms/pages#destroy
    page_parts GET    /page_parts(.:format)          minimalist_cms/page_parts#index
               POST   /page_parts(.:format)          minimalist_cms/page_parts#create
 new_page_part GET    /page_parts/new(.:format)      minimalist_cms/page_parts#new
edit_page_part GET    /page_parts/:id/edit(.:format) minimalist_cms/page_parts#edit
     page_part GET    /page_parts/:id(.:format)      minimalist_cms/page_parts#show
               PUT    /page_parts/:id(.:format)      minimalist_cms/page_parts#update
               DELETE /page_parts/:id(.:format)      minimalist_cms/page_parts#destroy

它有效,但如果我为此改变,在我的引擎中:

MinimalistCms::Engine.routes.draw do
  scope module: :minimalist_cms do
    root to: "pages#show"
    get ':id', to: "pages#show"
    resources :pages
    resources :page_parts
  end
end

在我的应用程序中:

像这样安装在我的虚拟应用程序中:

Dummy::Application.routes.draw do
  mount MinimalistCms::Engine, at: '/'
end

我有这个错误:

 Failure/Error: get :show, id: 'test'
 ActionController::RoutingError:
   No route matches {:id=>"test", :controller=>"minimalist_cms/pages", :action=>"show"}

结果rake routes似乎相似:

    minimalist        /                              MinimalistCms::Engine
          root        /                              minimalist_cms/pages#show
               GET    /:id(.:format)                 minimalist_cms/pages#show
         pages GET    /pages(.:format)               minimalist_cms/pages#index
               POST   /pages(.:format)               minimalist_cms/pages#create
      new_page GET    /pages/new(.:format)           minimalist_cms/pages#new
     edit_page GET    /pages/:id/edit(.:format)      minimalist_cms/pages#edit
          page GET    /pages/:id(.:format)           minimalist_cms/pages#show
               PUT    /pages/:id(.:format)           minimalist_cms/pages#update
               DELETE /pages/:id(.:format)           minimalist_cms/pages#destroy
    page_parts GET    /page_parts(.:format)          minimalist_cms/page_parts#index
               POST   /page_parts(.:format)          minimalist_cms/page_parts#create
 new_page_part GET    /page_parts/new(.:format)      minimalist_cms/page_parts#new
edit_page_part GET    /page_parts/:id/edit(.:format) minimalist_cms/page_parts#edit
     page_part GET    /page_parts/:id(.:format)      minimalist_cms/page_parts#show
               PUT    /page_parts/:id(.:format)      minimalist_cms/page_parts#update
               DELETE /page_parts/:id(.:format)      minimalist_cms/page_parts#destroy

应用程序的行为没有改变,它可以工作,但测试失败。

你有解决方案吗?

4

1 回答 1

0

也许不是使用范围,而是尝试以正常方式(没有范围)使用编写路由。

现在在您声明继承自 Rails::Engine 的引擎的文件中添加隔离命名空间。

Module MinimalistCms
   Class Engine < Rails::Engine
     isolate_namespace MinimalistCms
   end
end

现在尝试运行您的测试。

于 2014-02-19T12:30:44.257 回答