2012-10-15 19 views
0

失敗,這個問題可能已經問了十幾次上堆棧溢出(例如,(1),(2),(3),(4),(5)),但得到的答覆似乎是不同的,他們都沒有幫助我。我正在研究Rails引擎,我發現Rspec2獲取路由錯誤,但我可以在瀏覽器中訪問路由。這裏的情況:路線每次工作在瀏覽器中,在Rspec的

  • 在發動機的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+/} 
    
  • 而且我的測試,從控制器的規格之一(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 

...,就是這樣。

+0

這是控制器規格嗎? – zetetic

+0

@zetetic:是的。我想我應該這麼說 - 我現在要編輯添加。 – pjmorse

+0

你能在控制器中顯示你的新定義嗎? –

回答

1

我發現了一個辦法解決這個。在規格頂部,我添加了以下代碼:

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

...現在規格運行時沒有路由錯誤。但我不知道爲什麼這會起作用,所以如果有人可以發表解釋它的答案,我會接受。

0

我覺得可能是你越往上規格

一些錯誤是怎麼做的「輕量級」進入這一行:控制器=>「輕量級/ mw_interactives」

路線說

new_mw_interactive GET /mw_interactives/new(.:format) 

new_mw_interactive GET /lightweight/mw_interactives/new(.:format) 
+0

URL的「輕量級」部分是因爲控制器來自名稱空間引擎。 – pjmorse

+0

幾天後,我開始使用我的第一個rails引擎。如果我得到同樣的問題,我會回來。 –

0

添加文件規範/路由/ 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 

然後添加一個文件規範/控制器/ 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 
+0

爲什麼,它會如何回答我的問題?如果你問我是否有其他規格可以通過,答案是「是的,其中有幾十個。」 – pjmorse

相關問題