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 から継承するエンジンを宣言するファイルに、isolated_namespace を追加します。

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

今すぐテストを実行してみてください。

于 2014-02-19T12:30:44.257 に答える