Topic: Functional test and flash with an error

I'm getting this error thats moaning about expected tag, but no tag found matching {:content=>"User James Apps was successfully created.", :attributes=>{:id=>"notice"}, :tag=>"div"} in the file application.rb.

I tried using a regular expression - /#{user.first_name}/ , but that wouldn't work either

user_controller_test.rb

 1) Failure:
test_create(Admin::UserControllerTest)
    [/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/tag_assertions.rb:102:in `assert_tag'
     /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/tag_assertions.rb:99:in `clean_backtrace'
     /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/tag_assertions.rb:99:in `assert_tag'
     test/functional/admin/user_controller_test.rb:49:in `test_create'
     test/functional/admin/user_controller_test.rb:40:in `assert_difference'
     test/functional/admin/user_controller_test.rb:40:in `test_create']:
expected tag, but no tag found matching {:content=>"User James Apps was successfully created.", :attributes=>{:id=>"notice"}, :tag=>"div"} in:
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n<title>View Users</title>\n<link href=\"/stylesheets/dissertation.css?1174589813\" media=\"screen\" rel=\"Stylesheet\" type=\"text/css\" />\n</head>\n\n<body>\n<div id=\"header\">\n\t<div id=\"metaNav\">\n\t  <ul id=\"nav1\">\n\t   <li><a href=\"#\">User</a></li>\n\t   <li><a href=\"#\">Sign Out</a></li>\n\t   <li><a href=\"#\">Date</a></li>\n\t  </ul>\n\t </div>\n\t<h1>View Users</h1> <div id=\"mainNav\">\n  <ul id=\"nav2\">\n   <li><a href=\"#\">Projects</a></li>\n   <li><a href=\"#\">Settings</a></li>\n   <li><a href=\"#\">Contacts</a></li>\n  </ul>\n </div>\n</div>\n<div id=\"content\">\n<div id=\"right\">\n\t\t\t<div id=\"notice\">\n\t\t\t\t\t</div>\n\t<h1>View Users</h1>\n<p>From here you can view the users in the system.</p>\n<table>\n\t<tr>\n\t\t<th>Name</th>\n\t\t<th>Edit</th>\n\t\t<th>Delete</th>\n\t</tr>\n\t<tr>\n <td><a href=\"/admin/user/show/1\">James Apps</a></td>\n <td><a href=\"/admin/user/edit/1\">Edit</a></td>\n <td><form method=\"post\" action=\"/admin/user/destroy/1\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure you want to delete user James Apps?');\" type=\"submit\" value=\"Delete\" /></div></form>\n </td>\n</tr><tr>\n <td><a href=\"/admin/user/show/2\">Mark Faulkner</a></td>\n <td><a href=\"/admin/user/edit/2\">Edit</a></td>\n <td><form method=\"post\" action=\"/admin/user/destroy/2\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure you want to delete user Mark Faulkner?');\" type=\"submit\" value=\"Delete\" /></div></form>\n </td>\n</tr><tr>\n <td><a href=\"/admin/user/show/3\">Andy Jardine</a></td>\n <td><a href=\"/admin/user/edit/3\">Edit</a></td>\n <td><form method=\"post\" action=\"/admin/user/destroy/3\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure you want to delete user Andy Jardine?');\" type=\"submit\" value=\"Delete\" /></div></form>\n </td>\n</tr><tr>\n <td><a href=\"/admin/user/show/53\">James Apps</a></td>\n <td><a href=\"/admin/user/edit/53\">Edit</a></td>\n <td><form method=\"post\" action=\"/admin/user/destroy/53\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure you want to delete user James Apps?');\" type=\"submit\" value=\"Delete\" /></div></form>\n </td>\n</tr>\n</table>\n\n<p><a href=\"/admin/user/new\">Add a User</a></p>\n</div>\n\t\n<div id=\"left\">\n\t<div class=\"box\">\n\t\t\t<h2>News</h2>\t\n\t\t\t<p>Test, Test, Test, Test</p>\n\t</div>\n\t\t\t\n\t<div class=\"box\">\n\t <h2>Projects</h2>\n\t\t<ul>\n\t\t <li></li>\n\t\t <li></li>\n\t\t <li></li>\n\t\t</ul>\n\t</div>\n\t\t\n    <div class=\"box\">\n\t   \n\t</div>\n</div>\n</div>\n</body>\n</html>".
<nil> is not true.

user_controller.rb
def create
    # Created a user object rom the request parameters sent from the form on the new page.
    @user = User.new(params[:user])
    if @user.save
      # If user is valid then save and create a flash message then redirect to a page that lists the users
      flash[:notice] = "User #{@user.name} was successfully created."
      redirect_to :action => 'index'
    else
      # If user couldn't be saved (due to failing the validations) then the new page is rendered again.
      @page_title = 'Create new user'
      render :action => 'new'
    end
  end

application.rb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title><%= @page_title || 'Dissertation' -%></title>
<%= stylesheet_link_tag "dissertation" %>
</head>

<body>
<div id="header">
    <div id="metaNav">
      <ul id="nav1">
       <li><a href="#">User</a></li>
       <li><a href="#">Sign Out</a></li>
       <li><a href="#">Date</a></li>
      </ul>
     </div>
    <%= "<h1>#{@page_title}</h1>" if @page_title -%>
<div id="mainNav">
  <ul id="nav2">
   <li><a href="#">Projects</a></li>
   <li><a href="#">Settings</a></li>
   <li><a href="#">Contacts</a></li>
  </ul>
</div>
</div>
<div id="content">
<div id="right">
    <% if flash[:notice] -%>
        <div id="notice">
            <% flash[:notice] -%>
        </div>
    <% end -%>
<%= yield -%>

</div>
   
<div id="left">
    <div class="box">
            <h2>News</h2>   
            <p>Test, Test, Test, Test</p>
    </div>
           
    <div class="box">
     <h2>Projects</h2>
        <ul>
         <li></li>
         <li></li>
         <li></li>
        </ul>
    </div>
       
    <div class="box">
      
    </div>
</div>
</div>
</body>
</html>


Can anyone help, please

Re: Functional test and flash with an error

Looks like you're trying to use assert_tag on a flash message? That is made for testing the view, not flash messages. Try this instead:

# in test
assert_equal "User James was successfully created.", flash[:notice]

Railscasts - Free Ruby on Rails Screencasts

Re: Functional test and flash with an error

That has changed it but now it says:

 1) Error:
test_create(Admin::UserControllerTest):
ArgumentError: wrong number of arguments (1 for 2)
    test/functional/admin/user_controller_test.rb:49:in `assert_equal'
    test/functional/admin/user_controller_test.rb:49:in `test_create'
    test/functional/admin/user_controller_test.rb:40:in `assert_difference'
    test/functional/admin/user_controller_test.rb:40:in `test_create'

Re: Functional test and flash with an error

Try assert_match(/User James was successfully created/, flash[:notice])

BrewControl.com - Brewery and Brewpub management powered by RoR