I posted an article to my blog about this...
http://blog.wolfman.com/articles/2007/07/14/using-rspec-to-test-haml-helpers
On Jul 14, 3:53 pm, wolfmanjm <[EMAIL PROTECTED]> wrote:
> Excellent Thank you!! That worked like a charm...
>
> So to answer the persons original question...
>
> Use RSpec and this is an example of a complete helper rspec that tests
> the HTML generated...
>
> require File.dirname(__FILE__) + '/../spec_helper'
>
> describe ApplicationHelper do
> include ActionView::Helpers
> include Haml::Helpers
>
> it "should display flash" do
> @haml_is_haml = true
> @haml_stack = [Haml::Buffer.new(:attr_wrapper => "'")]
> for name in [:notice, :warning, :error]
> flash[name]= "flash #{name.to_s} message"
> capture_haml{
> display_flash
> }.should =~ /<div class='#{name.to_s}'>\s+#{flash[name]}\s+<\/
> div>/
> flash[name]= nil
> end
> end
> end
>
> for something in application_helper.rb...
>
> def display_flash
> for name in [:notice, :warning, :error]
> if flash[name]
> open :div, flash[name], {:class => name.to_s}
> end
> end
> nil
> end
>
> On Jul 14, 2:03 am, Nathan Weizenbaum <[EMAIL PROTECTED]> wrote:
>
> > This should work:
>
> > before :each do
> > @haml_is_haml = true
> > @haml_stack = [Haml::Buffer.new(:attr_wrapper => "'")]
> > end
>
> > However, because this is so useful, I've added it as a helper in trunk.
> > It would be easier to grab that and just run
>
> > before(:each) { init_haml_helpers }
>
> > Note that capture still won't work, due to some strange interactions (or
> > the lack thereof) between Haml and ActionView. Use capture_haml instead;
> > it does the same thing, but will work.
>
> > Let me know if this doesn't work.
>
> > - Nathan
>
> > wolfmanjm wrote:
> > > Ok I'm making progress :) Now I can actually post an error from within
> > > HAML!
>
> > > So I added this to my spec, which cleared up most of the previous
> > > errors...
>
> > > describe ApplicationHelper do
> > > include ActionView::Helpers
> > > include ActionView::Helpers::CaptureHelper
> > > include Haml::Helpers
> > > ...
>
> > > It now finds HAML and calls HAML#open it also can find capture.
>
> > > Now I get this error, presumably because the HAML buffer has not been
> > > initialized. So is there a call I shol dmake to initialize HAML in the
> > > spec test?
>
> > > You have a nil object when you didn't expect it!
> > > You might have expected an instance of Array.
> > > The error occurred while evaluating nil.[]
> > > /home/morris/work/snowdogsrus/rails/snowdogsrus/config/../vendor/
> > > plugins/haml/lib/haml/helpers.rb:276:in `buffer'
> > > /home/morris/work/snowdogsrus/rails/snowdogsrus/config/../vendor/
> > > plugins/haml/lib/haml/helpers.rb:259:in `open'
> > > /home/morris/work/snowdogsrus/rails/snowdogsrus/config/../app/
> > > helpers/application_helper.rb:15:in `display_flash'
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---