Forum: RSpec [Rails] specifying that a layout yields?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Brandt Kurowski (Guest)
on 2009-03-29 20:32
(Received via mailing list)
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?

Thanks,

Brandt
Matt W. (Guest)
on 2009-03-30 11:41
(Received via mailing list)
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.
http://beta.songkick.com
http://blog.mattwynne.net
Zach D. (Guest)
on 2009-03-30 18:26
(Received via mailing list)
On Sat, Mar 28, 2009 at 9:33 PM, Brandt Kurowski
<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
> http://rubyforge.org/mailman/listinfo/rspec-users
>



--
Zach D.
http://www.continuousthinking.com
http://www.mutuallyhuman.com
Brandt Kurowski (Guest)
on 2009-03-30 20:58
(Received via mailing list)
On Mar 30, 3:39 am, Matt W. <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
Brandt Kurowski (Guest)
on 2009-03-30 21:27
(Received via mailing list)
On Mar 30, 9:56 am, Zach D. <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
This topic is locked and can not be replied to.