When using flash.now[:blah] the flash will usually be directly rendered in your view, after which rails clears the flash.now flashes.
This means you can’t test them in your functional controller tests by going (say)
assert_equal 'error message', flash.now[:blah]
You can, however, test flash.now by using assert_tag to parse the HTML response generated by your view. For example, the following would look for the error message inside a div tag..
assert_tag :tag => "div", :child => /Failed to create user/
For more info, see the Ruby on Rails Manual page for
assert_tag
assert_tag will parse the response and requires valid XHTML. If you’re rendering an RJS template or XML, you can test @response.body:
assert_match(/Failed to create user/, @response.body)
assert_tag has been deprecated, but you can now test flash.now using assert_select:
assert_select "div.notification", "Expected message."
You can also look into the Rails plugin named Flashback , which solves this problem without requiring you to use assert_tag in your functional tests.
Using the above example, and assuming we are calling some random controller which supports the action named index, we could do something like the following:
flashback
get :index
assert_equal 'error message', flash.flashed[:blah]
Patching FlashNow to store flash.now messages into flash[:now_cache] and make it accessible through flash.now_cache method in your test cases.
More about it can be found at:
http://www.pluitsolutions.com/2008/01/22/testing-flashnow-in-rails/