运行一些Rspec测试时出现意外错误。他们是Rspec测试找不到匹配的路线
1) PeopleController redirects when loading root should redirect to the splash page Failure/Error: get '/'
ActionController::UrlGenerationError: No route matches {:action=>"/", :controller=>"people"}
...
2) PeopleController redirects when loading /people/show should redirect to the base person path Failure/Error: get '/show' #/show
ActionController::UrlGenerationError: No route matches {:action=>"/show", :controller=>"people"}
我不明白为什么Rspec找不到路线。
从控制器,people_controller.rb
:
class PeopleController < ApplicationController
...
def show
redirect_to people_path
end
def index
@people = Person.all
end
...
从Rspec的文件people_controller_spec.rb
:
describe PeopleController do
describe "redirects" do
context "when loading root" do
it "should redirect to the temp page" do
get '/'
last_response.should be_redirect
follow_redirect!
last_request.url.should include('/temp')
end
end
context "when loading /people/show" do
it "should redirect to the base people path" do
get '/people/show'
last_response.should be_redirect
follow_redirect!
last_request.url.should include('/people')
end
end
end
end
我的路线:
$ rake routes
Prefix Verb URI Pattern Controller#Action
...
person GET /people/:id(.:format) people#show
...
root GET / redirect(301, /temp)
routes.rb
:
Rails.application.routes.draw do
resources :temp
resources :people
# map '/' to be a redirect to '/temp'
root :to => redirect('/temp')
end
我错过了什么从测试中获得路线以匹配?我可以看到根检测不起作用,因为它在技术上不是由人员控制器处理的(我试图将其作为理智测试进行处理,只会让自己变得更加困惑),但/show
失败对我来说真的没有意义。
这是正确的答案,但此外,您可能需要查看[请求规格](https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec)作为控制器规格的替代(或者另外,但它们通常可以完全取代它们)。 –