开发者

How do I test whether my controller rendered the right layout on Rails 3?

Controller code:

class BooksController < ApplicationController
  def index
    @books = Book.all
    respond_to do |format|
      format.html do
        render 'index', :layout => 'topgun'
      end
    end
  end
end

How should I test this in the spec?

require 'spec_helper'

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

I checked this related post, but my response object does开发者_运维问答n't appear to have a layout attribute/method.


You may find the "Testing Controllers with RSpec" RailsCast and the official rspec-rails documentation helpful.

Looking at the code for assert_template (which is just what render_template calls), it looks like you should be able to do

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

though I'm not entirely sure that will work.


For RSpec 3:

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜