This project is archived and is in readonly mode.

#10 ✓resolved
Andreas Neuhaus

Immediate flash values not accessible in tests

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

(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

Comments and changes to this ticket

  • josh

    josh April 17th, 2008 @ 10:13 PM

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

  • Andreas Neuhaus

    Andreas Neuhaus April 18th, 2008 @ 02:08 PM

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

  • Andreas Neuhaus

    Andreas Neuhaus April 18th, 2008 @ 02:41 PM

    New patch with git metainfo

  • josh

    josh April 18th, 2008 @ 05:28 PM

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

  • Andreas Neuhaus

    Andreas Neuhaus April 19th, 2008 @ 08:30 PM

    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.

  • Andreas Neuhaus

    Andreas Neuhaus April 19th, 2008 @ 08:37 PM

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

  • Andreas Neuhaus

    Andreas Neuhaus April 21st, 2008 @ 09:32 AM

    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.

  • Michael Koziarski

    Michael Koziarski May 2nd, 2008 @ 11:36 PM

    • State changed from “invalid” to “open”
  • Nathan Wilmes

    Nathan Wilmes May 6th, 2008 @ 06:57 PM

    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.

  • josh

    josh May 8th, 2008 @ 05:51 AM

    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">.
    
  • Andreas Neuhaus

    Andreas Neuhaus May 9th, 2008 @ 10:29 PM

    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.

  • Michael Koziarski

    Michael Koziarski May 10th, 2008 @ 03:47 AM

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

  • Andreas Neuhaus

    Andreas Neuhaus May 10th, 2008 @ 05:07 PM

    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.

  • Repository

    Repository May 11th, 2008 @ 07:33 AM

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages