Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Immediate flash values not accessible in tests

#10

(from Trac http://dev.rubyonrails.org/ticke.... As far as I Know, this is still an issue)

I'm not sure if this is by intention, but if a controller assigns an immediate flash value (e.g. flash.now[:error] = 'Boom'), that value is not available in tests (flash[:error] is nil in the test). This makes it impossible to assert immediate flash messages.

Patch that adds a test to reproduce the problem attached

Reported by Andreas Neuhaus · April 16th, 2008 @ 06:07 PM

State: resolved
Milestone: none
Assigned to: josh josh
Importance: none

Activity

  1. josh
    josh
    • State changed from new to open
    • Assigned user set to josh

    I think you should have to test it with flash.now not just flash.

    assert_equal 'Invalid', request.session["flash"].now["notice"]
    # but I guess flash_now is okay too
    assert_equal 'Invalid', request.session["flash_now"].now["notice"]
    

    Not sure if that works already or not.

    April 17th, 2008 @ 10:13 PM

  2. Andreas Neuhaus
    Andreas Neuhaus

    I modified the test to assert_equal flash.now, but it still doesn't seem to work (patch attached).

    I also created a new rails project with a controller action that sets a flash.now message, but wasn't able to access that message in the corresponding functional test. It looks like flash.now is an ActionController::Flash::FlashNow object in the test, but its @flash is empty...

    April 18th, 2008 @ 02:08 PM

  3. Andreas Neuhaus
    Andreas Neuhaus

    New patch with git metainfo

    April 18th, 2008 @ 02:41 PM

  4. josh
    josh
    • State changed from open to invalid

    Flash nows aren't store in the session response. They are just pushed directly to the template. So the best way to test them would be with an "assert_select".

    If you come up with a clever implementation for testing them, please let me know.

    April 18th, 2008 @ 05:28 PM

  5. Andreas Neuhaus
    Andreas Neuhaus

    I dug a little deeper into this problem and found out, that the flash is swept in process_cleanup. A view can display immediate flash values, because it accesses the flash before being swept. However, with test assertions, process_cleanup is done before and therefore the immediate values have been swept already.

    So my goal was to make the same flash values available to the test that are used in views. Attached is a patch, that modified flash.rb to do the sweep before processing the request instead on cleanup, plus tests that make sure that normal flash values as well as immediate flash values can be asserted in tests.

    April 19th, 2008 @ 08:30 PM

  6. Andreas Neuhaus
    Andreas Neuhaus

    Ouch. Even though all tests pass fine, it seems that the flash isn't swept in real applications with this patch.

    April 19th, 2008 @ 08:37 PM

  7. Andreas Neuhaus
    Andreas Neuhaus

    Attached a new patch where flash sweeping is done after assign_shortcuts, so that @_session can be checked. The previous patch had a problem accessing the session since assign_shortcut wasn't run before.

    It now works as expected.

    Unfortunately, I can't reopen the ticket.

    April 21st, 2008 @ 09:32 AM

  8. Michael Koziarski
    Michael Koziarski
    • State changed from invalid to open

    May 2nd, 2008 @ 11:36 PM

  9. Nathan Wilmes
    Nathan Wilmes

    We took a slightly different approach to the same sorts of problems in ticket 121. Our goal with assert_showing_flash was to confirm that the app uses flash versus flash.now correctly.

    May 6th, 2008 @ 06:57 PM

  10. josh
    josh

    I'm getting some failing tests now. I have a feeling its related to one of Pratik's deprecate instance variables commits.

      1) Failure:
    test_flash(ComponentsTest)
        [./test/controller/components_test.rb:114:in `test_flash'
         ./test/../lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:59:in `__send__'
         ./test/../lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:59:in `run']:
    <"My stoney baby"> expected but was
    <"no flash">.
    

    May 8th, 2008 @ 05:51 AM

  11. Andreas Neuhaus
    Andreas Neuhaus

    Looks like my patched flash.rb wasn't aware of component processing. I'm not used to components, but they seem to work by calling process of the component's controller. Previously, that call swept the flash. I modified flash.rb to not sweep the flash if processing a component. I'm not sure why this didn't occur in earlier revisions of rails, but now it looks correct and all tests pass. New patch attached.

    May 9th, 2008 @ 10:29 PM

  12. Michael Koziarski
    Michael Koziarski

    Why does the new patch switch to if @_session rather than @_request?

    May 10th, 2008 @ 03:47 AM

  13. Andreas Neuhaus
    Andreas Neuhaus

    Mmh, not sure. I guess I accidently mixed it up. I changed it back to @_session (attached patch updated).

    I'm not sure if the @_session check is needed anyway (except for a tiny runtime improvement if sessions are disabled because the sweep method isn't run). I tried to remove the @_session check and all tests still passed.

    May 10th, 2008 @ 05:07 PM

  14. Repository
    Repository
    • State changed from open to resolved

    (from [74eed6290e63111d1aad2b181692a84f4f040aea]) Fixed flash.now not being accessible in tests. Flash sweeping is now done before action processing instead after

    Signed-off-by: Michael Koziarski

    [#10 state:resolved]

    http://github.com/rails/rails/co...

    May 11th, 2008 @ 07:33 AM