Forum: RSpec Expecting a block to return a specific value

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Guilherme Machado C. (Guest)
on 2009-03-22 01:48
(Received via mailing list)

I have the following method in a Rails view helper:

def title
  content_for(:title) do
    # some code which generates a title dynamically

How can I spec that the code inside the block returns the correct title?

Zach D. (Guest)
on 2009-03-22 02:47
(Received via mailing list)
2009/3/21 Guilherme Machado C. <removed_email_address@domain.invalid>:
> How can I spec that the code inside the block returns the correct title?
My preference is to let my view helpers be rendered in my view specs,
and I move the logic that constructs the title onto a presenter. Then
I can say things like this in my view spec:

it "should render the title" do
  thing = mock "thing presenter", :title => "foo"
  assign[:thing] = thing
  render "some.template"
  response.should contain("foo")

And I can have a presenter spec which verifies the title is built

describe ThingPresenter do
   describe '#title' do
      it "should return a concatenated title, user name, blah blah blah"
         # ....

There are more ways to accomplish this I know, but I've grown to
become extremely fond of the clean separation between constructing
markup and producing presentation content. It makes things easier to
spec IMO, gives things a good home, and allows me to stop looking at
content for instance variables dynamically generated by Rails.

If interested...

Zach D.
This topic is locked and can not be replied to.