この質問はおそらくStackOverflowで何十回も質問されています(例:(1)、(2)、(3)、(4)、(5))が、毎回答えが異なっているようで、どれも私を助けてくれませんでした。Railsエンジンで作業していて、Rspec2でルートエラーが発生することがわかりましたが、ブラウザーでルートにアクセスできます。状況は次のとおりです。
エンジンの
routes.rb
:resources :mw_interactives, :controller => 'mw_interactives', :constraints => { :id => /\d+/ }, :except => :show # This is so we can build the InteractiveItem at the same time as the Interactive resources :pages, :controller => 'interactive_pages', :constraints => { :id => /\d+/ }, :only => [:show] do resources :mw_interactives, :controller => 'mw_interactives', :constraints => { :id => /\d+/ }, :except => :show end
の抜粋出力
rake routes
:new_mw_interactive GET /mw_interactives/new(.:format) lightweight/mw_interactives#new {:id=>/\d+/} ... new_page_mw_interactive GET /pages/:page_id/mw_interactives/new(.:format) lightweight/mw_interactives#new {:id=>/\d+/, :page_id=>/\d+/}
そして、私のテストは、コントローラーの仕様の1つから(
describe Lightweight::MwInteractivesController do
):it 'shows a form for a new interactive' do get :new end
...この結果が得られます:
Failure/Error: get :new
ActionController::RoutingError:
No route matches {:controller=>"lightweight/mw_interactives", :action=>"new"}
...それでも、ブラウザでそのルートに移動すると、意図したとおりに機能します。
ここで何が欠けていますか?
ETA:アンドレアスが提起するポイントを明確にするために:これはRailsエンジンであるため、rspecは名前空間にエンジンのルートを含むダミーアプリケーションで実行されます。
mount Lightweight::Engine => "/lightweight"
...したがって、に示されているルートの前には。rake routes
が付いてい/lightweight/
ます。そのため、Rspecエラーに表示されるルートは、にあるルートと一致していないようですrake routes
。しかし、それはデバッグを余分なステップにします。
ETA2:ライアン・クラークのコメントに答えて、これは私がテストしているアクションです:
module Lightweight
class MwInteractivesController < ApplicationController
def new
create
end
...以上です。