[英]rspec-rails: Failure/Error: get “/” No route matches
試用 rspec-rails。 我收到一個奇怪的錯誤 - 沒有找到任何路線,即使我可以在運行 rails 時在瀏覽器中正常訪問它們。
我什至只用 /
Failure/Error: get "/"
ActionController::RoutingError:
No route matches {:controller=>"action_view/test_case/test", :action=>"/"}
不過,我絕對可以在瀏覽器中訪問 / 和其他資源。 設置 rspec 時有什么我可能錯過的嗎? 我將它放入 Gemfile 並運行 rspec:install。
謝謝你,B先生
編輯:這是我的測試
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
我認為這里並沒有真正改變任何東西。
據我所知,您正試圖將兩個測試合二為一。 在 rspec 中,這應該分兩步解決。 在一個規范中測試路由,在另一個規范中測試 controller。
所以,添加一個文件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
,我使用的是 shoulda 或顯着定義的擴展匹配器。
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 我分別測試每個組件(型號、controller、視圖、路由),我使用 cucumber 編寫高級測試以切分所有層。
希望這可以幫助。
您必須為 controller 測試describe
controller。 此外,由於您在 controller 測試中測試視圖的內容,而不是單獨的視圖規范,因此您必須使用render_views
。
describe SomeController, "GET /" do
render_views
it "does whatever" do
get '/'
response.should have_selector(...)
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.