在控制器规范中的已安装引擎上的RoutingError

时间:2013-04-13 20:09:11

标签: ruby-on-rails rspec routing

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

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

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

你有解决方案吗?

1 个答案:

答案 0 :(得分:0)

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

现在在您声明从Rails :: Engine继承的引擎的文件中添加isolated_namespace。

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

立即尝试运行测试。