Topic: assert_equal not finding flash[:notice]

I think I'm doing something very basic very wrong, but I can't see it.

In this functional test:

  def test_delete_user
    #should go back to list_users if there is no parameter
    get :delete_user, {}, :user_id => users(:chuck).id
    assert_redirected_to :action => 'list_users'
   
    post :delete_user, { :id => users(:frank).id }, :user_id => users(:chuck).id
    assert_redirected_to :action => 'list_users'
    follow_redirect
    assert_select 'b', 'chuckd'
    assert_select 'b', :text => /franks/, :count => 0
    assert_select 'div#notice', "User franks deleted"
    assert_equal "User franks deleted", flash[:notice]
  end

only the last assertion (line 12) fails.  The really odd thing is that the assertion before (line 11), which theoretically should be looking at the same thing, passes.

Here's the controller method:

  def delete_user
    unless params[:id].blank?
      username = User.find(params[:id]).username
      User.find(params[:id]).destroy
      flash[:notice] = "User #{username} deleted"
    end
    redirect_to :action => 'list_users' 
  end

Anyone know why that assertion is failing?  I'm also having similar issues in other tests.


Thanks,

c.

Re: assert_equal not finding flash[:notice]

What does the failure message say? When I do assert_equals on the flash and they don't match, I get an error like this:

<"Message"> expected but was <"Message2">.

Re: assert_equal not finding flash[:notice]

<"User franks deleted"> expected but was <nil>.

That's the part that has me just baffled.  How can it be nil?

Re: assert_equal not finding flash[:notice]

OK, the only thing I could think of is that you have two tests combined in one method. I've had bad luck with that before...apparently stuff from the first test gets left over for the second.

I went into one of my tests and added an additional test at the top and I got the same flash error that you did ("X expected but was <nil>").

So try this:

def test_delete_user_using_get
  # should go back to list_users if there is no parameter
  get :delete_user, {}, :user_id => users(:chuck).id
  assert_redirected_to :action => 'list_users'
end

def test_delete_user_using_post
  post :delete_user, { :id => users(:frank).id }, :user_id => users(:chuck).id
  assert_redirected_to :action => 'list_users'
  follow_redirect
  assert_select 'b', 'chuckd'
  assert_select 'b', :text => /franks/, :count => 0
  assert_select 'div#notice', "User franks deleted"
  assert_equal "User franks deleted", flash[:notice]
end

Re: assert_equal not finding flash[:notice]

Thanks for the reply, boone.

That's probably a good practice to do anyway, but in my case splitting up the get and post parts of the test as you suggested didn't help.  I'm still getting the same error.

Any other ideas?

Re: assert_equal not finding flash[:notice]

Well, rats. I though we had it solved. Are you sure you saved the test file before you ran the test again?

At this point it might be helpful to see your whole controller, model, functional test, and fixture.

Re: assert_equal not finding flash[:notice]

I may be wrong but I think the problem is that by the time you're inspecting flash[], it's already been cleared. See:

http://blog.codefront.net/2006/09/03/so … -on-rails/

This is a problem for me too, and I've yet to find a decent way around it.  The solution offered in the above blog post doesn't help because of the redirect; the only HTML I get is the 'redirecting' page :-(

Re: assert_equal not finding flash[:notice]

I've been revisiting my functional tests so am running into this issue again. 

The first thing that hit me is that I'm amazed that this isn't a more common problem since testing a flash notice seems pretty fundamental to me.

Besides that, replacing

assert_equals "Some error here", flash[:notice]

with

assert_tag :tag => div, :child => /Some error here/

does three things:

1. It allows some tests to run properly
2. Reminds you to use 'follow_redirect' if you have a redirect within the same controller
3. In general this serves as a really good troubleshooting tool since it shows you what your HTML output is.  In my case, it shows that the pages that don't work are just a redirect page, as duncan has also found.  e.g.:

expected tag, but no tag found matching {:tag=>"div", :child=>/You are not authorized to view the page you requested/} in:
"<html><body>You are being <a href=\"http://test.host/activity/alpha_welcome\">redirected</a>.</body></html>".
<nil> is not true.


Still looking for a good work around for testing redirects to other controllers...