[英]rspec-rails: Failure/Error: get “/” No route matches
Trying out rspec-rails.试用 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.
我收到一个奇怪的错误 - 没有找到任何路线,即使我可以在运行 rails 时在浏览器中正常访问它们。
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?
设置 rspec 时有什么我可能错过的吗? I put it into the Gemfile and ran rspec:install.
我将它放入 Gemfile 并运行 rspec:install。
Thank you, MrB谢谢你,B先生
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:我的 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.我认为这里并没有真正改变任何东西。
As far as i know, you are trying to combine two tests into one.据我所知,您正试图将两个测试合二为一。 In rspec this should be solved in two steps.
在 rspec 中,这应该分两步解决。 In one spec you test the routing, and in another you test the controller.
在一个规范中测试路由,在另一个规范中测试 controller。
So, add a file spec/routing/root_routing_spec.rb
所以,添加一个文件
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.然后添加一个文件
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
Actually, I almost never use render_views
but always test my components as isolated as possible.实际上,我几乎从不使用
render_views
,但总是尽可能孤立地测试我的组件。 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.使用 rspec 我分别测试每个组件(型号、controller、视图、路由),我使用 cucumber 编写高级测试以切分所有层。
Hope this helps.希望这可以帮助。
You have to describe
a controller for a controller test.您必须为 controller 测试
describe
controller。 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
.此外,由于您在 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.