Reputation: 18014
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 doesn't appear to have a layout
attribute/method.
Upvotes: 17
Views: 10399
Reputation: 4516
For RSpec 3:
expect(response).to render_template(:new) # wraps assert_template
https://relishapp.com/rspec/rspec-rails/v/3-7/docs/controller-specs
Upvotes: 1
Reputation: 96934
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.
Upvotes: 24