# Specifying a controller's layout

**URL:** <https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712>\
**Category:** RSpec\
**Created:** [September 4, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712 "2008-09-04T15:44:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [September 4, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/1 "2008-09-04T15:44:15Z")

</div>

I want to spec that a controller uses a particular layout

how do I do that?

## cheers, Matt

[http://blog.mattwynne.net](http://blog.mattwynne.net)

> **[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.

In case you wondered: The opinions expressed in this email are my own  
and do not necessarily reflect the views of any former, current or  
future employers of mine.

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [September 4, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/2 "2008-09-04T16:15:58Z")

</div>

On Thu, Sep 4, 2008 at 8:42 AM, Matt W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to spec that a controller uses a particular layout  
> how do I do that?

Depends on what else is going on, but this is the simplest situation:

controller.expect\_render(:layout =\> ‘special\_layout’)  
get :some\_action

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [September 4, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/3 "2008-09-04T16:31:14Z")

</div>

Thanks David.

I really struggled to get that to catch anything. My colleague Dan  
found this, which is working well for me:  
[http://rubyforge.org/pipermail/rspec-users/2007-May/001816.html](http://rubyforge.org/pipermail/rspec-users/2007-May/001816.html)

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [September 4, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/4 "2008-09-04T17:15:47Z")

</div>

noting my own typo  
def negeative\_failure\_message  
should be  
def negative\_failure\_message  
🙂

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [September 4, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/5 "2008-09-04T18:33:46Z")

</div>

On 4 Sep 2008, at 16:12, Jonathan L. wrote:

> noting my own typo  
> def negeative\_failure\_message  
> should be  
> def negative\_failure\_message  
> 🙂

Copy & Paste Considered Harmfull 😉

Thanks

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [October 16, 2008, 1:59pm UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/6 "2008-10-16T13:59:42Z")

</div>

David C. wrote:

> On Thu, Sep 4, 2008 at 8:42 AM, Matt W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I want to spec that a controller uses a particular layout  
> > how do I do that?
> 
> Depends on what else is going on, but this is the simplest situation:
> 
> controller.expect\_render(:layout =\> ‘special\_layout’)  
> get :some\_action

David:  
Why is it that

get :some\_action  
controller.expect\_render(:layout =\> ‘special\_layout’)

ie having the get statement before the controller.expect.. statement  
produces the ff error:  
Mock ‘expect\_render\_mock\_proxy’ expected :render with  
({:layout=\>“special\_layout”}) once, but received it 0 times

Thanks

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [October 16, 2008, 4:06pm UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/7 "2008-10-16T16:06:05Z")

</div>

Jim G. wrote:

> On Oct 16, 2008, at 7:59 AM, Dave Phiri wrote:
> 
> > David:  
> > Why is it that
> > 
> > get :some\_action  
> > controller.expect\_render(:layout =\> ‘special\_layout’)
> > 
> > ie having the get statement before the controller.expect.. statement  
> > produces the ff error:  
> > Mock ‘expect\_render\_mock\_proxy’ expected :render with  
> > ({:layout=\>“special\_layout”}) once, but received it 0 times
> 
> Because you are creating an expectation for the following action.  
> In your code above, you are doing the action, and then creating an  
> expectation for a following action which never occurs, so it receives  
> it 0 times following that expectation.
> 
> -Jim

Thanks Jim,  
It all now makes sense to me. I am barely a wk old in rspec. A lot many  
thanks!  
Dave

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [October 16, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/8 "2008-10-16T17:08:05Z")

</div>

On Thu, Oct 16, 2008 at 9:49 AM, Jim G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Because you are creating an expectation for the following action.  
> In your code above, you are doing the action, and then creating an  
> expectation for a following action which never occurs, so it receives it 0  
> times following that expectation.

This confused me too at first. To put it another way:

(1) controller.expect\_render(:layout =\> ‘index’) [set expectation]  
(2) get :index [perform action]

…is equivalent to…

(1) Call spouse to say “Honey, I’m coming home, see you shortly.”  
(2) Go home and kiss spouse.

Putting the “get” first and then setting the “expect” would be  
equivalent to:

(1) Go home and kiss spouse.  
(2) Call spouse to say “Honey, I’m coming home, see you shortly.”

Your spouse’s natural reaction would be “Ummmm…” Which is  
basically what RSpec is telling you by that error message when you do  
the ‘get’ then the ‘expect.’

–  
Have Fun,  
Steve E. ([removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid))  
ESCAPE POD - The Science Fiction Podcast Magazine  
[http://www.escapepod.org](http://www.escapepod.org)

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [October 16, 2008, 3:52pm UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/9 "2008-10-16T15:52:46Z")

</div>

On Oct 16, 2008, at 7:59 AM, Dave Phiri wrote:

> David:  
> Why is it that
> 
> get :some\_action  
> controller.expect\_render(:layout =\> ‘special\_layout’)
> 
> ie having the get statement before the controller.expect.. statement  
> produces the ff error:  
> Mock ‘expect\_render\_mock\_proxy’ expected :render with  
> ({:layout=\>“special\_layout”}) once, but received it 0 times

Because you are creating an expectation for the following action.  
In your code above, you are doing the action, and then creating an  
expectation for a following action which never occurs, so it receives  
it 0 times following that expectation.

-Jim

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [October 18, 2008, 5:41am UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/10 "2008-10-18T05:41:37Z")

</div>

On Thu, Oct 16, 2008 at 1:18 PM, Rahoul B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > (1) Call spouse to say “Honey, I’m coming home, see you shortly.”
> 
> def when\_getting action, parameters = {}  
> yield if block\_given?  
> get action, parameters  
> end
> 
> Likewise there is when\_posting\_to, when\_putting\_to and when\_deleting\_from
> 
> One day (soon) I’ll get round to packaging them up and submitting them as a  
> patch.

Cool. Don’t forget the seldom used #head, though I don’t know what to  
call it: when\_heading\_to?

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [October 16, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/11 "2008-10-16T17:35:42Z")

</div>

On 16 Oct 2008, at 16:02, Stephen E. wrote:

> This confused me too at first. To put it another way:
> 
> (1) controller.expect\_render(:layout =\> ‘index’) [set expectation]  
> (2) get :index [perform action]
> 
> …is equivalent to…
> 
> (1) Call spouse to say “Honey, I’m coming home, see you shortly.”  
> (2) Go home and kiss spouse.

I’ve written some helpers for this - nothing complex, just reverses  
the order that things happen in:

when\_getting :index do  
expect\_some\_stuff\_to\_happen  
end

The helper itself looks like this:

def when\_getting action, parameters = {}  
yield if block\_given?  
get action, parameters  
end

Likewise there is when\_posting\_to, when\_putting\_to and  
when\_deleting\_from

One day (soon) I’ll get round to packaging them up and submitting them  
as a patch.

Rahoul B.  
Web design and development: [http://www.3hv.co.uk/](http://www.3hv.co.uk/)  
Nottingham Forest: [http://www.eighteensixtyfive.co.uk/](http://www.eighteensixtyfive.co.uk/)  
Serious Rails Hosting: [http://www.brightbox.co.uk/](http://www.brightbox.co.uk/)  
Lifecast: [http://www.madeofstone.net/](http://www.madeofstone.net/)

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [October 18, 2008, 9:36am UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/12 "2008-10-18T09:36:05Z")

</div>

On Sat, Oct 18, 2008 at 2:44 AM, Nick H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On 2008-10-17, at 17:16, David C. wrote:
> 
> > Cool. Don’t forget the seldom used #head, though I don’t know what to  
> > call it: when\_heading\_to?
> 
> I looked through the docs at [http://rspec.rubyforge.org/rspec/1.1.8/](http://rspec.rubyforge.org/rspec/1.1.8/) for  
> #head , but the closest thing I found was #header , which is undocumented.  
> When you have a minute, would you mind telling us about #head please?

It’s a rails method, not rspec. Check out ActionController::Base#head.

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [October 18, 2008, 6:44am UTC](https://www.ruby-forum.com/t/specifying-a-controllers-layout/147712/13 "2008-10-18T06:44:46Z")

</div>

On 2008-10-17, at 17:16, David C. wrote:

> Cool. Don’t forget the seldom used #head, though I don’t know what to  
> call it: when\_heading\_to?

I looked through the docs at [http://rspec.rubyforge.org/rspec/1.1.8/](http://rspec.rubyforge.org/rspec/1.1.8/)  
for #head , but the closest thing I found was #header , which is  
undocumented. When you have a minute, would you mind telling us about  
#head please?

Thanks!  
Nick
