2016-04-29 8 views
1
RSpecの

とRailsの-チュートリアルを実行している間、私は第5章でテストを失敗した理由を理解していません。この本は最小のフレームワークを使用しましたが、私はRSpecを使うことにしました。これを行うために、私はテストフォルダを削除し、私のGemfileにrspec-railsを含めると、バンドルインストールを実行し、rails g rspec:installはスペックフォルダを生成します。しかし、static_pages_controller_spec.rbファイルには、assert_selectなどの最小限の構文で便利に動作すると感じるテストがいくつかあります。ここに私のspecファイルがどのように見えるかです:未定義のメソッド「文書」:NilClassは、私は<code>railstutorial by Michael Hartl</code>次午前

require "rails_helper" 

RSpec.describe StaticPagesController, type: :controller do 
    describe "GET #home" do 
    it "returns http success" do 
     get :home 
     expect(response).to have_http_status(:success) 
    end 
    it "should have the right title" do 
     get :home 
     assert_select "title", "Ruby on Rails Tutorial Sample App" 
    end 
    end 

    describe "GET #help" do 
    it "returns http success" do 
     get :help 
     expect(response).to have_http_status(:success) 
    end 
    it "should have the right title" do 
     get :help 
     assert_select "title", "Help | Ruby on Rails Tutorial Sample App" 
    end 
    end 

    describe "GET #about" do 
    it "returns http success" do 
     get :about 
     expect(response).to have_http_status(:success) 
    end 
    it "should have the right title" do 
     get "about" 
     assert_select "title", "About | Ruby on Rails Tutorial Sample App" 
    end 
    end 
end 

私はRSpecのでテストを実行すると、これは私が失敗のエラーとして得るものです:

StaticPagesController GET #home should have the right title 
Failure/Error: assert_select "title", "Ruby on Rails Tutorial Sample App" 

NoMethodError: 
    undefined method `document' for nil:NilClass 
# ./spec/controllers/static_pages_controller_spec.rb:11:in `block (3 levels) 
in <top (required)>' 

同じエラーメッセージ(No Method error)がそれぞれに表示されます失敗したテスト。

どうすれば修正できますか?私が間違っていることがありますか?

答えて

0

理由は、RSpecのは、デフォルトでは、コントローラの仕様のためのビューをレンダリングしないということです。

describe FooController, type: :controller do 
    render_views 

    # write your specs 
end 

をか、あなたのRSpecのの設定で、このどこかを追加することによって、グローバルにそれを有効にすることができます:あなたはこのような仕様の特定のグループのビューのレンダリングを有効にすることができ

RSpec.configure do |config| 
    config.render_views 
end 

は、より多くの情報のためhttps://www.relishapp.com/rspec/rspec-rails/v/2-6/docs/controller-specs/render-viewsを参照してください。

関連する問題