Hi,

Well, I googled some more, and everything I could find used eval_erb. So is
this the best method for testing helpers with blocks?

And then why is that block displaying when I run the test?

Thanks,
Brandon

> -----Original Message-----
> From: Brandon Olivares [mailto:programmer2...@gmail.com]
> Sent: Wednesday, April 08, 2009 6:25 AM
> To: 'rspec-users'
> Subject: Testing helpers that accept a block
> 
> Hi,
> 
> I am trying to write and test a helper that accepts a block. Right now
> it goes something like this:
> 
> module ContactHelper
> 
>   # Wraps a field with div.error if it has an error.
>   def wrap_error_field errors, &block
>     field = capture &block
>     if errors.empty?
>       concat field
>     else
>       concat content_tag(:div, field, :class => 'error')
>     end
>   end # field_error_helper
> 
> This is my first time writing a helper, so please tell me if I'm wrong.
> 
> Then, I tried to test it, but couldn't figure it out, since it seems
> the method won't actually return anything due to the use of concat. The
> best I could figure out for now was to use eval_erb.
> 
> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
> 
> describe ContactHelper do
> 
>   describe "field_error_helper" do
> 
>     context "when there is an error" do
> 
>       it "should wrap the block in div.error" do
> 
>         assigns[:errors] = [
>           'Error'
>         ]
> 
>         output = eval_erb <<-ERB
>           <% wrap_error_field @errors do %>
>             <p>Block</p>
>             <% end %>
>         ERB
> 
>         assert_xhtml output do
>           div.error do
>             p 'Block'
>           end
>         end
> 
>       end # it should wrap the block in div.error
> 
>     end # when there is an error
> 
>   end # describe field_error_helper
> 
> end
> end
> 
> Is that the best/only method? I'm not sure how much I really like it.
> 
> Further, when I run the tests, it always outputs "Block" before the
> test is executed, and I can't figure out why, and that's slightly
> annoying.
> 
> Thanks,
> Brandon

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to