[rspec-users] Testing layouts with RSpec on Rails

David Chelimsky dchelimsky at gmail.com
Mon Oct 22 05:49:50 EDT 2007


On 10/22/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> On 10/21/07, Steve <vertebrate at gmail.com> wrote:
> > On Tue, 09 Oct 2007 21:12:09 +0100, Matt Patterson wrote:
> >
> > > It occured to me that it would be nice to have a spec that would barf
> > > if I ever nuked the layout's yield (or, indeed, one of its
> > > content_for yields), mainly because they're the integration point
> > > between layouts and views, and we're primarily testing them in
> > > isolation (a good thing...)
> > >
> > > I was mainly wondering if anyone had some great practices in testing
> > > those integration points, and layout stuff in general, that I could
> > > learn from...
> > >
> > > Matt
> >
> > Was there ever any resolution on this? This would be a *very* beneficial
> > capability to have for making sure views work as expected.
>
> It would be awesome if you could just do this:
>
> $yielded = false
> render "/layouts/application.html.erb" do
>   $yielded = true
> end
> $yielded.should be_true
>
> However, it doesn't work :(
>
> Basically, someone with more time on his/her hands than I needs to
> figure out how to observe the yield call and verify that it happened
> after the fact (as opposed to using a message expectation).
>
> Patches welcome!

Actually, it turns out that you can do this in a view spec:

render "/path/to/my/file.html.erb", :layout => "application"

and it will render with the layout. So you *could* (in theory, I
haven't done this yet) do something like this:

# spec/resources/views/layout_example.html.erb
<div>yielded from layout</div>

# spec/views/layouts/application.html.erb_spec.rb
require File.dirname(__FILE__) + '/../../spec_helper'

describe "/layouts/application" do

  it "should yield" do
    render "/../../spec/resources/views/layout_example.html.erb",
:layout => "application"
    response.should have_tag('div','yielded from layout')
  end

  it "should have title" do
    render "/../../spec/resources/views/layout_example.html.erb",
:layout => "application"
    response.should have_tag("div.idbar","My Killer App")
  end
  ...
end


More information about the rspec-users mailing list