Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3311070
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:50:01+00:00 2026-05-17T21:50:01+00:00

I wanted to test a method in my helper class but when I do

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:50:02+00:00Added an answer on May 17, 2026 at 9:50 pm

    You can only use flash[:notice] in your controllers to set them, and in your views to render them. The Flash is declared in ActionController::Flash.

    When you create your own Flash Hash (flash = {}), it works okay, but that flash variable is not made available to your helper in any way, hence the error again.

    If you want to test your flash messages correctly, I suggest you write test with a tool like Cucumber: Perform an action, and expect to see some text in the HTML response. Works great.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1) OnCreate is public method of instantiated object from ClsLast class. But I wanted
I wanted to code a helper method in Unit test project, which will initialize
I wanted to write this small, trivial extension method for some throwaway test /
Installed visual studio 11 beta as wanted to test EF 5 beta but keep
I am developing an API and wanted to test the login method. The spec
I wanted to test how C++ behaves when the return value of a function
Out of curiosity I wanted to test the number of ticks to compare a
I set up a new webapp Maven project and wanted to test it with
Hi I've been writing a chat client and wanted to test the Java Sound
I have built part of my app and wanted to test it out. All

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.