thanks, M
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.
- Nathan
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.
Thanks
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
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
Are they just expecting vanilla Strings back from the calls?
-hampton.
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 <hcat...@gmail.com> wrote:
> Yeah, the Helpers being tested definitely aren't loading Haml in.
>
> Are they just expecting vanilla Strings back from the calls?
>
> -hampton.
>
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.
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'
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
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
http://blog.wolfman.com/articles/2007/07/14/using-rspec-to-test-haml-helpers
If the helper was arrived at by extraction[1], it's already been
covered elsewhere and you don't need to test it again.
-Steven
1. If it wasn't, you're overengineering. Stop it.