Forum: RSpec Expecting a block to return a specific value

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.
7217ceaad90d7ae913d8742ccdd697f2?d=identicon&s=25 Guilherme Machado Cirne (Guest)
on 2009-03-22 00:48
(Received via mailing list)
Hi,

I have the following method in a Rails view helper:

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

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

TIA,
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 Zach Dennis (zdennis)
on 2009-03-22 01:47
(Received via mailing list)
2009/3/21 Guilherme Machado Cirne <gcirne@gmail.com>:
> 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")
end

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

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

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...   http://wiki.github.com/mhs/caching_presenter


--
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com
This topic is locked and can not be replied to.