2011-04-27 145 views
6

試用rspec-rails。我得到一個奇怪的錯誤 - 即使在運行rails s的瀏覽器中可以正常訪問它們,也不會找到路由。rspec-rails:失敗/錯誤:get「/」沒有路由匹配

我甚至只/

Failure/Error: get "/" 
    ActionController::RoutingError: 
     No route matches {:controller=>"action_view/test_case/test", :action=>"/"} 

嘗試它,我絕對可以訪問/和其他資源的瀏覽器,但。在設置rspec時有什麼我可能錯過的?我把它放進Gemfile並運行rspec:install。

謝謝 MRB

編輯:這是我的測試

1 require 'spec_helper' 
    2 
    3 describe "resource" do 
    4 describe "GET" do 
    5  it "contains /" do 
    6  get "/" 
    7  response.should have_selector("h1", :content => "Project") 
    8  end 
    9 end 
10 end 

這是我的路由文件:

myApp::Application.routes.draw do 

    resources :groups do 
    resources :projects 
    end 

    resources :projects do 
    resources :variants 
    resources :steps 

    member do 
     get 'compare' 
    end 
    end 

    resources :steps do 
    resources :costs 
    end 

    resources :variants do 
    resources :costs 
    end 

    resources :costs 

    root :to => "home#index" 

end 

我spec_helper.rb:

ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails'  

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

RSpec.configure do |config| 

    config.mock_with :rspec 
    config.include RSpec::Rails::ControllerExampleGroup 


    config.fixture_path = "#{::Rails.root}/spec/fixtures" 


    config.use_transactional_fixtures = true 
end 

沒真的改變這裏的任何東西,我想。

+0

張貼你的路線文件? – tbaums 2011-04-28 13:51:32

+0

你可以發佈你的spec_helper.rb嗎? – moritz 2011-05-03 13:12:01

回答

4

就我所知,你正試圖將兩個測試合併爲一個。在rspec中,這應該分兩步解決。在一個規範中測試路由,在另一個規範中測試控制器。

所以,添加一個文件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 

    render_views 

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

    it "has the right title" do 
     response.should have_selector("h1", :content => "Project") 
    end 

    end 
end 

其實,我幾乎從不使用render_views,但總是測試我的組件儘可能孤立。該視圖是否包含我在我的視圖規範中測試的正確標題。

使用rspec的我測試單獨的每個組件(模型,控制器,視圖,路由),以及i用黃瓜寫高電平測試穿過所有層切片。

希望這會有所幫助。

+0

不錯!只是嘗試了路由和這似乎工作!謝謝! – MrB 2011-05-04 07:36:09

2

您必須爲describe控制器進行控制器測試。此外,由於您正在測試控制器測試中的視圖內容,而不是單獨的視圖規範,因此您必須render_views

describe SomeController, "GET /" do 
    render_views 

    it "does whatever" do 
    get '/' 
    response.should have_selector(...) 
    end 
end 
+0

這似乎也沒有幫助,/仍然沒有發現。我其實並不想專門測試一個控制器。我只想測試我是否能夠獲得正確的視圖,所以我猜測它比視圖控制器測試更像是視圖測試。或者也許是一體的。但無論是將控制器進入形容也不把「render_views」在:-(幫助 – MrB 2011-04-28 07:35:30

+0

如果它是一個視圖規範,那麼你不應該在所有訪問路徑。見http://relishapp.com/rspec/rspec-rails/v/2-5/DIR /視圖功能/視圖規格的例子。 – 2011-04-28 17:17:47

相關問題