如何测试我的控制器是否在Rails 3上呈现正确的布局?

控制器代码:

class BooksController  'topgun' end end end end 

我应该如何在规范中测试这个?

 require 'spec_helper' describe BooksController do describe "GET index" do it "renders the topgun layout" do get :index # ??? end end end 

我检查了这个相关的post ,但我的response对象似乎没有layout属性/方法。

您可能会发现“使用RSpec测试控制器” RailsCast和官方rspec-rails文档很有帮助。

查看assert_template的代码(这正是render_template调用的代码),看起来你应该能够做到

 response.should render_template("index") response.should render_template(:layout => "topgun") 

虽然我不完全确定这会奏效。

对于RSpec 3:

 expect(response).to render_template(:new) # wraps assert_template 

https://relishapp.com/rspec/rspec-rails/v/3-7/docs/controller-specs