2011-04-27 143 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

相关问题