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

Ste*_*ven 17 rspec ruby-on-rails

控制器代码:

class BooksController < ApplicationController
  def index
    @books = Book.all
    respond_to do |format|
      format.html do
        render 'index', :layout => 'topgun'
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

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

require 'spec_helper'

describe BooksController do
  describe "GET index" do
    it "renders the topgun layout" do
      get :index
      # ???
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

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

And*_*all 23

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

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

response.should render_template("index")
response.should render_template(:layout => "topgun")
Run Code Online (Sandbox Code Playgroud)

虽然我不完全确定它会起作用.