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?
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.