[rspec-users] Expecting a block to return a specific value

Zach Dennis zach.dennis at gmail.com
Sat Mar 21 20:22:02 EDT 2009

2009/3/21 Guilherme Machado Cirne <gcirne at gmail.com>:
> 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?

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 correctly

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

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

More information about the rspec-users mailing list