Guilherme Machado Cirne
2009-Mar-21 23:38 UTC
[rspec-users] Expecting a block to return a specific value
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, -- Guilherme Machado Cirne gcirne at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20090321/61c61d02/attachment.html>
Zach Dennis
2009-Mar-22 00:22 UTC
[rspec-users] Expecting a block to return a specific value
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") 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