Unable to access flash from rails helper spec since upgrade to 1.1.4

Hi,

Many moons ago I wrote this blog entry…

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

I just upgraded that rails project to use rspec 1.1.4 and associated
rspec_rails.

It seems that my helper when called from the helper specs no longer are
able to access flash,
controller etc as they did before.

I already prepended all helper calls with helper. so I fixed those
changes, but I have been unable
to figure out how to access flash etc…

Here is a simplified example…

in ApplicationHelper.rb

def test_flash
for name in [:notice, :warning, :error]
if flash[name]
return “#{flash[name]}”
end
end
nil
end

In my rails/spec/helpers/application_helper_spec.rb

require File.dirname(FILE) + ‘/…/spec_helper’

describe ApplicationHelper do

it “should test flash” do
for name in [:notice, :warning, :error]
flash[name]= “flash #{name.to_s} message”
helper.test_flash.should match(/.#{name}./)
flash[name]= nil
end
end

end

I get this error trace…
1)
NoMethodError in ‘ApplicationHelper should test flash’
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.[]
/…/app/helpers/application_helper.rb:23:in `test_flash’

flash is nil, this used to work, so what is the new magic incantation to
get this to work again?

Thanks

On Jul 2, 2008, at 7:12 PM, Jim M. wrote:

are able to access flash, controller etc as they did before.
for name in [:notice, :warning, :error]

end
flash is nil, this used to work, so what is the new magic
incantation to get this to work again?

Hey Jim - I fixed this in
http://github.com/dchelimsky/rspec-rails/commit/40a0916325dbe9a1631849146d078e330fca71f2
, so you can either grab the very latest from github or wait for the
1.1.5 release (coming in the next few days).

Cheers,
David

Hi David,

Thanks that worked, and fixed that problem.

Now I have run into another problem, from the same blog example (which
I’ll update once I get it all
to work).

Within the application helper I call haml_tag, which works fine when the
app runs. (used to be
called open).

However when I try to test that helper in my helper spec I get…

NoMethodError in ‘ApplicationHelper should display flash’
undefined method `haml_tag’ for
#Spec::Rails::Example::HelperExampleGroup::HelperObject:0xb71ced20

For some reason it doesn’t know anything about haml when run from the
spec.

I guess I need to inject a require haml or something into the
HelperObject? I just don’t know how to
do that.

I am using the latest version of HAML but I am still using rails 1.2.6

Any ideas?

Thanks
Jim

On Jul 3, 2008, at 1:29 AM, Jim M. wrote:

However when I try to test that helper in my helper spec I get…

NoMethodError in ‘ApplicationHelper should display flash’
undefined method `haml_tag’ for
#Spec::Rails::Example::HelperExampleGroup::HelperObject:0xb71ced20

For some reason it doesn’t know anything about haml when run from
the spec.

I guess I need to inject a require haml or something into the
HelperObject? I just don’t know how to do that.

Try this:

before(:each) do
helper.extend Haml::Helpers
end

it “should …” do

end

Ok, its messy but this works…

before :each do
init_haml_helpers
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(/

\s*#{flash[name]}\s*</div>/)
flash[name]= nil
end
end

NOTE that the capture haml needs to be prepended by helper. otherwise
nothing gets returned.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs