Forum: RSpec Testing helpers that accept a block

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.
F85bacbbd4814799d4526b3e35a431df?d=identicon&s=25 Brandon Olivares (Guest)
on 2009-04-08 12:57
(Received via mailing list)
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
F85bacbbd4814799d4526b3e35a431df?d=identicon&s=25 Brandon Olivares (Guest)
on 2009-04-09 20:20
(Received via mailing list)
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
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-04-09 20:55
(Received via mailing list)
>         assert_xhtml output do
>           div.error do
>             p 'Block'
>           end
>         end

Nokogiri has some "easter eggs" in its ambitious HTML::Builder system.
One of
them is only HTML tags not already recognized as methods can get turned
into
HTML tags. The usual .method_missing() abuse.

The fix is:

         assert_xhtml output do
           div.error do
             p! 'Block'
           end
         end

(Also .be_html_with might work by now... 0.4.9?)

Aaron P. is pondering this issue even now in his subconscious, but I
only ought
to feed him one feature request at a time and a couple others are ahead
in the
queue, so my ! trick has to work for the near future...
F85bacbbd4814799d4526b3e35a431df?d=identicon&s=25 Brandon Olivares (Guest)
on 2009-04-10 00:04
(Received via mailing list)
Hi,

Oh, I should have thought of that. Thanks a lot.

I'm growing used to the assert_xhtml syntax, lol, so I've just been
using
that. Thank you though for fixing the be_html_with syntax.

Brandon
This topic is locked and can not be replied to.