2016-11-16 7 views
0

一部の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 

テストからのルートを取得するには、何が欠けていますか?私は根本的なテストがPeople Controllerによって技術的には処理されていないので正常に機能しないことが分かりました(私は健全性テストとしてそれを入れて、もっと混乱させました)。

答えて

2

コントローラのテストでは、方法getはパスではなくアクション引数をとります。リソースがメンバー(コレクションではなく)であれば、あなたもidパラメータを指定する必要があり、そう:

get :show, id: 1 

は{IDを含むparamsハッシュで、PeopleControllerのインスタンスに#showアクションを呼び出します。 '1'}。

これは、Guide to Testing Rails Applicationsでより詳細に説明されています。

+0

これは正解ですが、さらに[要求仕様](https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec)を参照してください。 (またはそれに加えて、それらを完全に置き換えることもできます)。 –