I wanted to test a method in my helper class but when I do something like:
require 'test_helper'
class ApplicationHelperTest < ActionView::TestCase
def test_flash_messages
flash[:notice] = "Hello World"
assert_equal "<div class=\"message-notice\">Hello World<\/div>", flash_messages
end
end
than I get "NoMethodError: undefined method `flash' for nil:NilClass"
But when I do something like:
flash = {}
flash[:notice] = "Hello World"
assert_equal "<div class=\"message-notify\">Hello World<\/div>", flash_messages
I get the same error message but it is called in "application_helper.rb:6:in `flash_messages'"
By the way my application_helper looks like:
module ApplicationHelper
def flash_messages
fl = ''
flash.each do |key,msg|
if flash[key]
fl = fl + "<div class=\"message-#{key}\">#{msg}</div>"
end
flash[key] = nil
end
return fl
end
end
This works on the website, but I want to be able to test this kind of method with unit test.