【问题标题】:rspec-rails: Failure/Error: get "/" No route matchesrspec-rails:失败/错误:获取“/”没有路由匹配
【发布时间】:2011-08-13 21:07:02
【问题描述】:

试用 rspec-rails。我收到一个奇怪的错误 - 没有找到任何路线,即使我在运行 rails s 时可以在浏览器中正常访问它们。

我什至只用 / 试过了

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

我认为这里并没有真正改变任何东西。

【问题讨论】:

  • 你能发布你的 spec_helper.rb 吗?

标签: ruby-on-rails rspec routes


【解决方案1】:

据我所知,您正在尝试将两个测试合二为一。在 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,我用的是should或显着定义的扩展匹配器。

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 我分别测试每个组件(模型、控制器、视图、路由),并使用 cucumber 编写高级测试以切分所有层。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您必须 describe 控制器进行控制器测试。此外,由于您在控制器测试中测试视图的内容,而不是单独的视图规范,因此您必须 render_views

    describe SomeController, "GET /" do
      render_views
    
      it "does whatever" do
        get '/'
        response.should have_selector(...)
      end
    end
    

    【讨论】:

    • 这似乎也没有帮助,/仍然没有找到。我实际上并没有尝试专门测试控制器。我只是想测试我是否得到正确的视图,所以我想这更像是视图测试而不是控制器测试。或者也许是一体的。但是将控制器放入描述或将“render_views”放入帮助中都没有帮助:-(
    • 如果它是视图规范,那么您根本不应该访问路线。有关示例,请参阅relishapp.com/rspec/rspec-rails/v/2-5/dir/view-specs/view-spec
    猜你喜欢
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多