[rspec-users] [Rails] specifying that a layout yields?

Zach Dennis zach.dennis at gmail.com
Mon Mar 30 09:56:46 EDT 2009

On Sat, Mar 28, 2009 at 9:33 PM, Brandt Kurowski
<brandtkurowski at gmail.com> wrote:
> I recently had a bug in a layout due to the layout not calling "yield"
> to display the actual content. I wanted to write a spec to describe
> what was missing, but it wasn't obvious how, so I just fixed the
> problem first and doubled back to spec it later.
> Anyway, the most succinct thing I was able to come up with was the
> following:
>  # http://gist.github.com/87246
>  describe "/layouts/application" do
>    it "should show the content" do
>      class << template
>        attr_accessor :did_render_content
>      end
>      template.did_render_content = false
>      render :inline => "<% self.did_render_content = true
> %>", :layout => 'application'
>      template.did_render_content.should be_true
>    end
>  end
> But I'm not quite satisfied with this, as it's not as clear and
> expressive as specs usually are. I thought I'd write a custom matcher
> for it, but it's not obvious to me what I'd even want the spec to look
> like.
> Any ideas?

There's no need for a custom matcher. You just utilize the :layout
option that render has. e.g. the follow example ensures the layout
renders the content it is given (via a yield block):

describe "layouts/application.html.erb" do
  it "should render the context given" do
    render :text => "foobar", :layout => "layouts/application.html.erb"
    response.should include_text("foobar")

> Thanks,
> Brandt
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Zach Dennis

More information about the rspec-users mailing list