require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') module TestHelperModule end describe TestHelperModule do describe "simple block_helper" do before(:each) do module TestHelperModule remove_const(:TestHelper) if defined?(TestHelper) class TestHelper < BlockHelpers::Base def hello 'Hi there' end end end end it "should make the named helper available" do helper.should respond_to(:test_helper) end it "should work for a simple yielded object" do eval_erb(%( <% test_helper do |h| %>
Before
<%= h.hello %>After
<% end %> )).should match_html("Before
Hi thereAfter
") end it "should do nothing if no block given" do eval_erb(%( <% test_helper %> )).should match_html("") end it "should return itself (the renderer object)" do eval_erb(%( <% e = test_helper %> <%= e.hello %> )).should match_html('Hi there') end end describe "access to other methods" do before(:each) do module TestHelperModule def yoghurt 'Yoghurt' end remove_const(:TestHelper) if defined?(TestHelper) class TestHelper < BlockHelpers::Base def yog yoghurt[0..2] end def jelly_in_div content_tag :div, 'jelly' end def cheese helper.cheese[0..3] end def label_tag(text) helper.label_tag(text[0..1]) end def check_capture(&block) string = capture(&block) 2.times{ concat(string) } end end def cheese 'Cheese' end end end it "should give the yielded renderer access to other methods" do eval_erb(%( <% test_helper do |r| %> <%= r.yog %> <% end %> )).should match_html("Yog") end it "should give the yielded renderer access to normal actionview helper methods" do eval_erb(%( <% test_helper do |r| %> <%= r.jelly_in_div %> <% end %> )).should match_html("Before
#{body}After
) end end end end end it "should surround a simple block" do eval_erb(%( <% test_helper_surround do %> Body here!!! <% end %> )).should match_html("Before
Body here!!!After
") end it "should pass in the body as nil if no block given" do eval_erb(%( <% test_helper_surround %> )).should match_html("This is nil!") end end describe "block helpers with arguments" do before(:each) do module TestHelperModule remove_const(:TestHelperWithArgs) if defined?(TestHelperWithArgs) class TestHelperWithArgs < BlockHelpers::Base def initialize(id, klass) @id, @klass = id, klass end def hello %(Hello
) end end end end it "should use the args passed in" do eval_erb(%( <% test_helper_with_args('hello', 'there') do |r| %> <%= r.hello %> <% end %> )).should match_html(%(Hello
)) end end end