0

この質問はおそらく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

...以上です。

4

3 に答える 3

1

この回避策を見つけました。仕様の一番上に、次のコードを追加しました。

render_views
before do
  # work around bug in routing testing
  @routes = Lightweight::Engine.routes
end

...そして今、スペックはルーティングエラーなしで実行されます。しかし、なぜこれが機能するのかわからないので、誰かがそれを説明する答えを投稿できれば、私はそれを受け入れます。

于 2012-10-16T14:11:34.563 に答える
0

私はあなたのスペックの上の何かが間違っているかもしれないと思います

「lightweight」はどのようにしてこの行に入りましたか:controller => "lightweight / mw_interactives"

ルートは言う

new_mw_interactive GET    /mw_interactives/new(.:format)

いいえ

new_mw_interactive GET    /lightweight/mw_interactives/new(.:format)
于 2012-10-15T19:20:11.857 に答える
0

ファイルspec/routing/root_routing_spec.rbを追加します

require "spec_helper"

describe "routes for Widgets" do
  it "routes /widgets to the widgets controller" do
    { :get => "/" }.should route_to(:controller => "home", :action => "index")
  end
end

次に、ファイルspec / controllers/home_controller_spec.rbを追加します

require 'spec_helper'

describe HomeController do

    context "GET index" do
    before(:each) do
      get :index
    end
    it {should respond_with :success }
    it {should render_template(:index) }

   end
end  
于 2012-10-15T19:28:14.940 に答える