# \[Rails\] specifying that a layout yields?

**URL:** <https://www.ruby-forum.com/t/rails-specifying-that-a-layout-yields/164185>\
**Category:** RSpec\
**Created:** [March 29, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/rails-specifying-that-a-layout-yields/164185 "2009-03-29T18:32:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brandt\_Kurowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@Brandt\_Kurowski](https://www.ruby-forum.com/u/Brandt_Kurowski)\
**Post date:** [March 29, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/rails-specifying-that-a-layout-yields/164185/1 "2009-03-29T18:32:53Z")

</div>

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:

# [87246’s gists · GitHub](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?

Thanks,

Brandt

---

<div class="post-metadata">

**Author:** ![Brandt\_Kurowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@Brandt\_Kurowski](https://www.ruby-forum.com/u/Brandt_Kurowski)\
**Post date:** [March 30, 2009, 9:41am UTC](https://www.ruby-forum.com/t/rails-specifying-that-a-layout-yields/164185/2 "2009-03-30T09:41:59Z")

</div>

On 29 Mar 2009, at 02:33, Brandt Kurowski wrote:

> it “should show the content” do  
> 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?
> 
> Thanks,
> 
> Brandt

That’s some pretty nifty code Brandt 🙂

Did you consider testing this aspect of the view’s behaviour from a  
higher-level Cucumber acceptance test? I think that’s what I would  
have probably done.

Matt W.

> **[Find the Best Concerts Near You, Tour Dates & Tickets Worldwide | Songkick](https://www.songkick.com/)**
>
> Find live music near you, track your favorite artists, get instant concert alerts and buy tickets for all the best upcoming concerts.

> **[Tea-Driven Development – Matt Wynne taking it one tea at a time](http://blog.mattwynne.net)**

---

<div class="post-metadata">

**Author:** ![Brandt\_Kurowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@Brandt\_Kurowski](https://www.ruby-forum.com/u/Brandt_Kurowski)\
**Post date:** [March 30, 2009, 4:26pm UTC](https://www.ruby-forum.com/t/rails-specifying-that-a-layout-yields/164185/3 "2009-03-30T16:26:02Z")

</div>

On Sat, Mar 28, 2009 at 9:33 PM, Brandt Kurowski  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> it “should show the content” do  
> 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”)  
end  
end

> Thanks,
> 
> Brandt
> 
> * * *
> 
> rspec-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

–  
Zach D.  
[http://www.continuousthinking.com](http://www.continuousthinking.com)

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com)**
>
> We empower businesses to overcome challenges with a holistic focus on People, Process, and Technology for lasting results and innovation.

---

<div class="post-metadata">

**Author:** ![Brandt\_Kurowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@Brandt\_Kurowski](https://www.ruby-forum.com/u/Brandt_Kurowski)\
**Post date:** [March 30, 2009, 7:27pm UTC](https://www.ruby-forum.com/t/rails-specifying-that-a-layout-yields/164185/4 "2009-03-30T19:27:30Z")

</div>

On Mar 30, 9:56 am, Zach D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > template.did_render_content.should be_true
> > 
> > ```
> 
> ```
> response.should include_text("foobar")
> 
> ```
> 
> end  
> end

Doh! Looks like I was so focused on the mechanism behind the failure  
that I ended up writing my spec for the details of the implementation  
rather than just the desired behavior. Thanks for pointing out what in  
retrospect should’ve been obvious Zach.

Brandt

---

<div class="post-metadata">

**Author:** ![Brandt\_Kurowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@Brandt\_Kurowski](https://www.ruby-forum.com/u/Brandt_Kurowski)\
**Post date:** [March 30, 2009, 6:58pm UTC](https://www.ruby-forum.com/t/rails-specifying-that-a-layout-yields/164185/5 "2009-03-30T18:58:40Z")

</div>

On Mar 30, 3:39 am, Matt W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 29 Mar 2009, at 02:33, Brandt Kurowski 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.
> 
> Did you consider testing this aspect of the view’s behaviour from a  
> higher-level Cucumber acceptance test? I think that’s what I would  
> have probably done.

Thanks Matt. It was actually a Cucumber feature that found the bug  
(this is a new project that I’ve yet to actually open a browser on, I  
think), so in that sense it’s already covered. But I wanted to somehow  
specify this very particular piece of behavior so that in the future  
if someone breaks it, there’s a spec that points right to the problem.  
I think that if someone breaks my layout this way (e.g. accidentally  
removing the yield in a future edit), most (all?) of my acceptance  
tests will fail but it won’t be immediately obvious why.

Brandt
