That is correct, and well within the paradigm that RSpec sets out, so
I agree that would be the best solution to test this helper.

However the original question at the top of the page was how to test
the actual HTML generated by Haml#open. presuming you don't trust Haml
to do the right thing ;) it would be nice to know how to test a helper
end to end through Haml to the generated HTML.

I would certainly like to know how to do it.

Another way to test end to end (and may go against RSpec methodology,
but does work well) is to simply test a view that uses the helper, and
test the generated HTML, which I have done and it works well. But a
more focused test on a specific helper would also be a nice tool in my
toolbox.

On Jul 13, 12:12 pm, Sean Cribbs <[EMAIL PROTECTED]> wrote:
> You might be able to mock that out. It looks like your spec bleeds too
> much into Haml and ActionView's domain.  Here's what I might do:
>
> it "should display flash" do
>   [:notice, :warning, :error].each do |name|
>     message = "flash #{name.to_s} message"
>     flash.should_receive(:[]).with(name).and_return(message)
>     MyHelper.should_receive(:open).with(:div, message, :id => name.to_s)
> # change to match your helper name
>     display_flash
>   end
> end
>
> Now, you may want to do something with the MyHelper.should_receive line
> and have it return something, which you could verify after the call.
>
> Sean Cribbs
>
> wolfmanjm wrote:
> > Well they used to :) Now I am trying to use the capture {...} syntax
> > as Nathan suggests, that would work fine, if it could find the capture
> > method, and the haml libraries for the open ... method.
>
> >   it "should display flash" do
> >     for name in [:notice, :warning, :error]
> >       flash[name]= "flash #{name.to_s} message"
> >       capture{
> >         display_flash
> >       }.should == "<div class=\"#{name.to_s}\">#{flash[name]}</div>"
> >       flash[name]= nil
> >     end
> >   end
>
> > Gives me and error...
> > undefined method `capture'
>
> > On Jul 13, 7:49 am, Hampton <[EMAIL PROTECTED]> wrote:
>
> >> Yeah, the Helpers being tested definitely aren't loading Haml in.
>
> >> Are they just expecting vanilla Strings back from the calls?
>
> >> -hampton.
>
> >> On 7/13/07, wolfmanjm <[EMAIL PROTECTED]> wrote:
>
> >>> I get...
>
> >>> undefined method `capture' for [Dynamically generated class for RSpec
> >>> example]
>
> >>> >From the spec, what should I require in the spec to get all the HAML
> >>> goodness?
>
> >>> Thanks
>
> >>> On Jul 12, 7:31 pm, Nathan Weizenbaum <[EMAIL PROTECTED]> wrote:
>
> >>>> That's really weird. I can't see anywhere that it would be trying to use
> >>>> a Symbol as a String. However, I do know how to get "open" as a string.
> >>>> The trick is that "open" and "puts," like "form_for" and the like, write
> >>>> directly to Haml's output buffer. However, there's a helper that allows
> >>>> you to take something directly written to the buffer and get it as a
> >>>> string, called "capture". It works like so:
>
> >>>> capture do
> >>>>   open :foo, 'bar'
> >>>> end
>
> >>>> That would return "<foo>\n  bar\n</foo>". So you could do "capture {
> >>>> display_flash }.should == ...".
>
> >>>> Try that and see if it works. If it doesn't, let me know.
>
> >>>> - Nathan
>
> >>>> wolfmanjm wrote:
>
> >>>>> So presuming I can require haml or something and overcome this error,
> >>>>> how would U check the output? Obviously this spec was written
> >>>>> expecting the display_flash helper to return a string (which it used
> >>>>> to do), by using open it now presumably renders directly to the haml
> >>>>> buffer, so how would I retrieve that so I can test it? (Remember this
> >>>>> helper spec runs without using actually using a view).
>
> >>>>> Thanks
>
> >>>>> On Jul 11, 10:13 pm, wolfmanjm <[EMAIL PROTECTED]> wrote:
>
> >>>>>> Sure here you go...
>
> >>>>>> TypeError in 'ApplicationHelper should display flash'
> >>>>>> can't convert Symbol into String
> >>>>>> /home/morris/work/snowdogsrus/rails/snowdogsrus/config/../app/helpers/
> >>>>>> application_helper.rb:15:in `open'
> >>>>>> /home/morris/work/snowdogsrus/rails/snowdogsrus/config/../app/helpers/
> >>>>>> application_helper.rb:15:in `display_flash'
> >>>>>> /home/morris/work/snowdogsrus/rails/snowdogsrus/config/../app/helpers/
> >>>>>> application_helper.rb:13:in `each'
> >>>>>> /home/morris/work/snowdogsrus/rails/snowdogsrus/config/../app/helpers/
> >>>>>> application_helper.rb:13:in `display_flash'
> >>>>>> /home/morris/work/snowdogsrus/rails/snowdogsrus/spec/helpers/
> >>>>>> application_helper_spec.rb:12:
> >>>>>> /home/morris/work/snowdogsrus/rails/snowdogsrus/spec/helpers/
> >>>>>> application_helper_spec.rb:10:in `each'
> >>>>>> /home/morris/work/snowdogsrus/rails/snowdogsrus/spec/helpers/
> >>>>>> application_helper_spec.rb:10:
>
> >>>>>> # application_helper_spec.rb
> >>>>>> require File.dirname(__FILE__) + '/../spec_helper'
>
> >>>>>> describe ApplicationHelper do
>
> >>>>>>   it "should display flash" do
> >>>>>>     for name in [:notice, :warning, :error]
> >>>>>>       flash[name]= "flash #{name.to_s} message"
> >>>>>>       display_flash.should == "<div class=\"#{name}\">#{flash[name]}</
> >>>>>> div>" # Line 12
> >>>>>>       flash[name]= nil
> >>>>>>     end
> >>>>>>   end
> >>>>>> end
>
> >>>>>> # application_helper.rb
>
> >>>>>>   def display_flash
> >>>>>>     for name in [:notice, :warning, :error]
> >>>>>>       if flash[name]
> >>>>>>         open :div, flash[name], {:class => name} # Line 15
> >>>>>>       end
> >>>>>>     end
> >>>>>>     nil
> >>>>>>   end
>
> >>>>>> (Note I know this test will not match, but it is meant to fail the
> >>>>>> first time ;)
> >>>>>> However the error has nothing to do with the test, it is because open
> >>>>>> is probably trying to call File::open
>
> >>>>>> I don't believe HAML is even being loaded for this test.
>
> >>>>>> On Jul 11, 6:19 pm, Nathan Weizenbaum <[EMAIL PROTECTED]> wrote:
>
> >>>>>>> Do you have a backtrace I can see? I'm too lazy to do it myself :p. In
> >>>>>>> my defense, it's the hottest day of the year in Seattle...
>
> >>>>>>> - Nathan
>
> >>>>>>> wolfmanjm wrote:
>
> >>>>>>>> I like RSpec as it has the ability to test helpers standalone.
>
> >>>>>>>> However I have not yet found the magic incantation to get it to work
> >>>>>>>> with a helper that uses open or puts.
>
> >>>>>>>> I suspect you need to include HAML and setup some context for it to
> >>>>>>>> render to.
>
> >>>>>>>> Any ideas on how to do that would be appreciated as I had to disable
> >>>>>>>> many of my RSpec helper tests when I started using HAML open.
>
> >>>>>>>> On Jul 10, 11:46 pm, Moses Hohman <[EMAIL PROTECTED]> wrote:
>
> >>>>>>>>> What's the best way to test helper methods that generate HTML using
> >>>>>>>>> Haml::Helpers#open? I want to test the helper method directly 
> >>>>>>>>> without
> >>>>>>>>> using it in a view.
>
> >>>>>>>>> thanks, M


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Haml" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/haml?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to