Testing HAML in helper rspec

Thought I’d post this under its own heading so others can find it.

Thanks to David for the pointers to solve this.

Here is the incantation needed to use HAML 2.0 under RSpec 1.1.5 (Needs
the .5 for a fix)

application_helper_spec.rb under spec/helpers

before :each do
helper.extend Haml
helper.extend Haml::Helpers
helper.send :init_haml_helpers
end

it "should display flash" do
  for name in [:notice, :warning, :error]
    flash[name]= "flash #{name.to_s} message"
    helper.capture_haml{
      helper.display_flash
    }.should match(/<div 

class=‘#{name.to_s}’>\s*#{flash[name]}\s*</div>/)
flash[name]= nil
end
end

Which tests this in application_helpers.rb

def display_flash
for name in [:notice, :warning, :error]
if flash[name]
haml_tag :div, flash[name], {:class => name.to_s}
end
end
nil
end

I also updated my blog post on this topic to cover the new syntax…

http://blog.wolfman.com/articles/2007/07/14/using-rspec-to-test-haml-helpers