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

前端 未结 2 1695
陌清茗
陌清茗 2021-01-04 00:31

Controller code:

class BooksController < ApplicationController
  def index
    @books = Book.all
    respond_to do |format|
      format.html do
        r         


        
相关标签:
2条回答
  • 2021-01-04 00:46

    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.

    0 讨论(0)
  • 2021-01-04 00:52

    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 讨论(0)
提交回复
热议问题