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 9012597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:02:36+00:00 2026-06-16T03:02:36+00:00

I’m not sure how to check flash messages with functional tests. My problem has

  • 0

I’m not sure how to check flash messages with functional tests. My problem has something to do with redirect_to because my test for flash[:error] DOES pass. Here is what my create action looks like:

def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to @user, flash: { success: "Welcome!" }
  else
    flash.now[:error] = "Signup failed:"
    render 'new'
  end
end

I have a passing test for flash[:error] that 1) checks that flash[:error] is the correct value, 2) checks the page for the flash div, and 3) gets a new page and checks that the flash message has been cleared. I tried to make an analogous test for flash[:success] but it is failing when it tries to find the div on the page. Here is the failing test:

  should "show flash[:success] message" do
    post :create, user: { name: "valid user",
                          password: "userpw",
                          password_confirmation: "userpw",
                          email: "a@b.com" }        
    assert_redirected_to user_path(assigns(:user)), "user isn't being redirected to their page after registration"
    assert_equal flash[:success], "Welcome!", "flash[:success] isn't being set properly"
    assert_select "div.alert-success", flash[:success]
    get :new
    assert flash.empty?, "there shouldn't be any flash messages left after getting new page"
  end

When I test this manually in a browser, it behaves as expected, but I’m not sure how to write the tests to reflect this. The first two assertions are passing (so the test recognizes that it is being redirected correctly, and that flash[:success] is being set to "Welcome!"). Based on that, I would think the assert_select statement would be able to find the div, but if fails with this error message:

Expected at least 1 element matching "div.alert-success", found 0.

So I tried adding puts @response.body right before the assert_select statement, which printed this:

<html><body>You are being <a href="http://test.host/users/3">redirected</a>.</body></html>

This explains why it couldn’t find the flash div, but I’m not sure how to “finish” the redirect so that it renders the correct view with the flash div. If I add a get statement right before the assert_select statement, the test passes, because now @response.body has the flash div. However, this doesn’t seem like an effective test because it doesn’t reflect the behavior of my application – the user doesn’t have to make a new request to see the flash message.

It also doesn’t guarantee that the flash message shows up on the correct page. If I use the statement get :new, the assertion still passes, which implies that the flash message would be present on the users#new view. Even if I use

get :show, id: assigns(:user).id  

to make sure the flash message shows up on the correct page, this still feels wrong because 1) now it is a success response instead of a redirect response, and 2) it still doesn’t explain why I have to manually request ANY page to find the flash div – according to the docs, “The flash is a special part of the session which is cleared with each request.“

Anyway, is there a way to test that a flash message shows up correctly after a redirect?

  • 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-06-16T03:02:38+00:00Added an answer on June 16, 2026 at 3:02 am

    I’m answering this question from the POV that what you’re trying to test would be better tested elsewhere. If you’re having a lot of trouble setting up the test, odds are there’s a better way to test it 🙂

    1. Testing that flash messages get cleared – flash messages are built into Rails, so it doesn’t make a lot of sense to test something that the framework is handling for you.

    2. Testing that elements show up on the page – view specs don’t belong in controllers. Here is a description of the role of the controller:

      A controller can thus be thought of as a middle man between models and
      views. It makes the model data available to the view so it can display
      that data to the user.

      The controller is supposed to make data available to the view, not to specify how the data gets rendered. Imagine if you changed the name of your CSS class, then you’d have to also update your controller specs.

      If you really want to test that flash messages show up, I would use an integration test.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I have an array which has BIG numbers and small numbers in it. I
I've tracked down a weird MySQL problem to the two different ways I was

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.