rspec-rails:失败/错误:获取“/”没有路线匹配
尝试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您正在尝试将两个测试合并为一个。在 rspec 中,这应该分两步解决。在一种规范中,您测试路由,在另一种规范中,您测试控制器。
因此,添加一个文件spec/routing/root_routing_spec.rb,
然后添加一个文件spec/controllers/home_controller_spec.rb,我正在使用由shoulda或定义的扩展匹配器卓越。
实际上,我几乎从不使用 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
And then add a file
spec/controllers/home_controller_spec.rb
, and I am using the extended matchers defined by shoulda or remarkable.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.
您必须
描述
控制器才能进行控制器测试。此外,由于您正在控制器测试中测试视图的内容,而不是单独的视图规范,因此您必须render_views
。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 torender_views
.