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
-~----------~----~----~----~------~----~------~--~---