2010-12-12 6 views
12

特定のレイアウトがRSpecで使用されていることをテストするにはどうすればよいですか?私は幸運とtemplate.layout、response.layout、およびresponse.should render_template( "レイアウト")を試みました。 RSpecの2でRspecテストレイアウト

答えて

17

、コントローラの仕様では、あなたが推測として、あなたはrender_templateを使用しますが、ビューのディレクトリからの相対パスを含める必要があります。あなたのレイアウトがアプリ/ビュー/レイアウト/ mylayout.html.erbがあるのであれば、あなたのスペックは次のようになります。

response.should render_template "layouts/mylayout" 
+0

レイアウトが使用されていないことをどのようにテストしますか?例えば、コントローラが「render:layout => false」とします。それをどうやってテストするのですか? –

1
# rspec-rails-1.3.x for rails-2 
describe HomeController do 
    describe "the home page" do 
    it "should use the :home_page layout" do 
     get :index 
     response.layout.should == "layouts/home_page" 
    end 
    end 
end 

# rspec-2 for rails-3 
describe "GET index" do 
    it "renders the page within the 'application' layout" do 
    get :index 
    response.should render_template 'layouts/application' # layout 
    response.should render_template 'index'    # view 
    end 
end 
+1

https://gist.github.com/11080d61648aaee51840私はrspec 2、レール3です。私はresponse.layoutと推奨されないtemplate.layoutエラーで廃止されました。 –

+0

私はあなたのフィードバックに基づいて私の答えを更新しました。 –

5

をまた、あなたは両方をテストすることができ、ワンライナーでレイアウトとアクションレンダリング、中RSpecの-2:

response.should render_template(%w(layouts/application name_of_controller/edit)) 
5

RSpecの3のための更新構文:

expect(response).to render_template(:index) # view 
expect(response).to render_template(layout: :application) # layout 

RSpec docs

それとも、@Flov's one-linerを好む、あなたが書くことができる場合:

expect(response).to render_template(:index, layout: :application) 

注意をassert_templateからrender_template委譲すること。これらのドキュメントはActionController assert_templateにあります。

+0

ちょうどメモ、これはもう働かないし、ドキュメントもsavagely古いです。 – Sebastialonso