rspec-rails:失败/错误:获取“/”没有路线匹配

发布于 2024-11-03 18:42:44 字数 1539 浏览 0 评论 0原文

尝试 rspec-rails。我收到一个奇怪的错误 - 据称没有找到路由,即使我在运行 Rails 时可以在浏览器中很好地访问它们。

我什至尝试过只使用 /

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

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

Trying out rspec-rails. I get a weird error - no routes are supposedly found, even though I can access them fine in the browser when running rails s.

I even tried it with just /

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

I can definitely access / and other resources in the browser, though. Is there something I could've missed when setting rspec up? I put it into the Gemfile and ran rspec:install.

Thank you,
MrB

edit: Here's my test

  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

Here's my route file:

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

My 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

Didn't really change anything here, I think.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光倒影 2024-11-10 18:42:44

据我所知,您正在尝试将两个测试合并为一个。在 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,我正在使用由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,我分别测试每个组件(模型、控制器、视图、路由),并使用 Cucumber 编写高级测试来切片所有层。

希望这有帮助。

As far as i know, you are trying to combine two tests into one. In rspec this should be solved in two steps. In one spec you test the routing, and in another you test the controller.

So, add a file 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

And then add a file spec/controllers/home_controller_spec.rb, and I am using the extended matchers defined by shoulda or remarkable.

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  

Actually, I almost never use render_views but always test my components as isolated as possible. Whether the view contains the correct title I test in my view-spec.

Using rspec i test each component (model, controller, views, routing) separately, and i use cucumber to write high level tests to slice through all layers.

Hope this helps.

旧梦荧光笔 2024-11-10 18:42:44

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

describe SomeController, "GET /" do
  render_views

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

You have to describe a controller for a controller test. Also, since you're testing the contents of the view in the controller test and not a separate view spec, you have to render_views.

describe SomeController, "GET /" do
  render_views

  it "does whatever" do
    get '/'
    response.should have_selector(...)
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文