l'essentiel est invisible pour les yeux

Sunday, May 18, 2008

[rails] RSpec render_layout expects render specified layout file

Original idea via http://rubyforge.org/pipermail/rspec-users/2007-October/004026.html

Add some code into your spec/spec_helper.rb is as follows:


class RenderLayout
def initialize(expected)
@expected = 'layouts/' + expected
end

def matches?(controller)
@actual = controller.layout
@actual == @expected
end

def failure_message
return "render_layout expected #{@expected.inspect}, got #{@actual.inspect}", @expected, @acutual
end

def negative_failure_message
return "render_layout expected #{@expected.inspect} not to equal #{@actual.inspect}", @expected, @actual
end
end

def render_layout(expected)
RenderLayout.new(expected)
end

your_controller_spec.rb

it "should render 'application' layout file" do
get :index
response.should render_layout('application')
end