Topic: Test Failing...Why

Heres the Errors, why are these errors happening?

Loaded suite test/functional/admin/user_controller_test
Started
.ignoring attempt to close p with br
  opened at byte 74, line 6
  closed at byte 124, line 6
  attributes at open: {}
  text around open: " method=\"post\">\n\t\n\n\n<p><label for=\"user_"
  text around close: "\">First name</label></br>\n\t\n\t<input id=\""
ignoring attempt to close p with br
  opened at byte 230, line 10
  closed at byte 278, line 10
  attributes at open: {}
  text around open: "ue=\"James\" /></p>\n\t\n<p><label for=\"user_"
  text around close: "e\">Last name</label></br>\n\t\n\t<input id=\""
.Fignoring attempt to close p with br
  opened at byte 80, line 6
  closed at byte 130, line 6
  attributes at open: {}
  text around open: "ethod=\"post\">\t\n\t\t\n\n\n<p><label for=\"user_"
  text around close: "\">First name</label></br>\n\t\n\t<input id=\""
ignoring attempt to close p with br
  opened at byte 222, line 10
  closed at byte 270, line 10
  attributes at open: {}
  text around open: "ype=\"text\" /></p>\n\t\n<p><label for=\"user_"
  text around close: "e\">Last name</label></br>\n\t\n\t<input id=\""
.F
Finished in 0.166395 seconds.

  1) Failure:
test_index(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:71:in `test_index'
     test/functional/admin/user_controller_test.rb:69:in `each'
     test/functional/admin/user_controller_test.rb:69:in `test_index']:
expected tag, but no tag found matching {:tag=>"td", :content=>"James"} in:
"<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/index/1?user=show\">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/index/2?user=show\">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/index/3?user=show\">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>\n</table>\n\n<p><a href=\"/admin/user/new\">Add a User</a></p>".
<nil> is not true.

  2) Failure:
test_show(Admin::UserControllerTest) [test/functional/admin/user_controller_test.rb:82]:
<"James"> expected but was
<"Apps">.

5 tests, 16 assertions, 2 failures, 0 errors


user_controller_test.rb
# Functional Test file for user_controller
require File.dirname(__FILE__) + '/../../test_helper'
require 'admin/user_controller'

# Re-raise errors caught by the controller.
class Admin::UserController; def rescue_action(e) raise e end; end

class Admin::UserControllerTest < Test::Unit::TestCase
  fixtures :users # Loads up the fixture file and inserts the data into the database
  def setup
    @controller = Admin::UserController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end

  # Test case for new method
  def test_new
    get :new  # Get test helper method to call the new action
    assert_template 'admin/user/new'  # Test whether the correct view is rendered
    assert_tag 'h1', :content => 'Create new user'  # Test the content via the h1
    # Testing whether a form element points to /admin/user/create
    assert_tag 'form', :attributes => {:action => '/admin/user/create'}
  end
 
  # Old Test case for create method
  #def test_create
  #  assert_equal 0 , User.find(:all).size # Ensures that there are no users in the test database
  #  post :create, :user => {:first_name => 'James', # Create a new User by simulating the HTTP POST method
  #                         :last_name => 'Apps'} # sending a form to create action
  #  assert_response :redirect # Ensures a redirect
  #  assert_redirected_to :action => 'index' # Ensures that the redirect is to the index page
  #  assert_equal 1, User.find(:all).size  # Ensures that a new User object
  #  assert_equal 'User James Apps was successfully created.', flash[:notice]  # Has Rails set the flash variable correctly
  #end
 
  # New Test case for create method that uses the helper method in test/test_helper.rb
  def test_create
    get :new
    assert_template 'admin/user/new'
    assert_difference(User, :count) do
      post :create, :user => {:first_name => 'James',
                                :last_name => 'Apps' }
      assert_response :redirect
      assert_redirected_to :action => 'index'
  end
  assert_equal 'User James Apps was successfully created.', flash[:notice]
end

  # Test case for validation, same as above but only use the first_name
  def test_failing _create
    # do not create a user
    assert_no_difference(User, :count) do
      post :create, :user => {:first_name => 'James'}
      assert_response :success
      assert_template 'admin/user/new'
      # Testing for a <div class="fieldWithErrors"> tag
      assert_tag :tag => 'div', :attributes => {:class => 'fieldWithErrors'}
  end
end

  def test_index
    get :index 
    # check if index action returns successful HTTP response
    assert_response :success 
    # check if index page has a table rows of users, header row and table cells
    assert_tag :tag => 'table',
               :children => { :count => User.count + 1,
                              :only => {:tag => 'tr'} }
    User.find(:all).each do |a|
        assert_tag :tag => 'td',
                   :content => a.first_name
        assert_tag :tag => 'td',
                    :content => a.last_name
    end
  end
 
  def test_show
    get :show, :id => 1  # Request the show page
    assert_template 'admin/user/show' # Test the right template is called
    # assigns(). helper method Checks instance variable to see if its the right one
    assert_equal 'James', assigns(:user).first_name
    assert_equal 'James', assigns(:user).last_name
  end
 
  def test_edit
    get :edit, :id => 1 # Load up the edit page for the first author
    assert_tag :tag => 'input', # Test the forms for the create input fields
               :attributes => { :name => 'user[first_name]',
                                :value => 'James' }
    assert_tag :tag => 'input',
              :attributes => { :name => 'user[last_name]',
                               :value => 'Apps'}
  end
end

Re: Test Failing...Why

Looks like you have "</br>" in your view code. Try replacing it with "<br />"

Railscasts - Free Ruby on Rails Screencasts

Re: Test Failing...Why

I've removed those but there still seems to be one error left:

Loaded suite test/functional/admin/user_controller_test
Started
..F..
Finished in 0.127659 seconds.

  1) Failure:
test_index(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:71:in `test_index'
     test/functional/admin/user_controller_test.rb:69:in `each'
     test/functional/admin/user_controller_test.rb:69:in `test_index']:
expected tag, but no tag found matching {:tag=>"td", :content=>"James"} in:
"<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>\n</table>\n\n<p><a href=\"/admin/user/new\">Add a User</a></p>".
<nil> is not true.

5 tests, 16 assertions, 1 failures, 0 errors

Re: Test Failing...Why

Heres the test_index out of the user_controller_test.rb
The error in the previous post stipulates that it cant find the td tag doesn't it?


 def test_index
    get :index 
    # check if index action returns successful HTTP response
    assert_response :success 
    # check if index page has a table rows of users, header row and table cells
    assert_tag :tag => 'table',
               :children => { :count => User.count + 1,
                              :only => {:tag => 'tr'} }
    User.find(:all).each do |a|
        assert_tag :tag => 'td',
                   :content => a.first_name
        assert_tag :tag => 'td',
                   :content => a.last_name
    end
  end

Re: Test Failing...Why

You may need to place the "content" part in a regular expression so it does a partial search. Right now I believe it's looking for a "td" tag which contains only James and nothing else:

assert_tag :tag => 'td', :content => /#{a.first_name}/

Railscasts - Free Ruby on Rails Screencasts

Re: Test Failing...Why

So could I just use this: %r{^[A-Za-z]$}, and implement it like this:

   1.  def test_index
   2.     get :index 
   3.     # check if index action returns successful HTTP response
   4.     assert_response :success 
   5.     # check if index page has a table rows of users, header row and table cells
   6.     assert_tag :tag => 'table',
   7.                :children => { :count => User.count + 1,
   8.                               :only => {:tag => 'tr'} }
   9.     User.find(:all).each do |a|
  10.         assert_tag :tag => 'td',
  11.                    :content => %r{^[A-Za-z]$}

  12.         assert_tag :tag => 'td',
  13.                    :content => %r{^[A-Za-z]$}

  14.     end
  15.   end

Re: Test Failing...Why

That will just check for a <td> tag with one alpha character inside of it. I don't understand what you are trying to do here. Have you tried the code I gave before?

Railscasts - Free Ruby on Rails Screencasts

Re: Test Failing...Why

Oh no I didn't sorry I completely missed that, thank you I will try it.

Re: Test Failing...Why

It works now thank you for your help, however I'm a little confused, as I read that a regular expression looks like this: %r{^[A-Za-z]$}.

Does this /#{a.first_name}/ search for whats stored inside of first_name variable then? What do the "/# /" do?

Thank you, for your help.

Re: Test Failing...Why

The #{} portion just tells Ruby to insert this ruby code into the regular expression. In other words, the end result is this:

assert_tag :tag => 'td', :content => /James/

You could also write it like this:

assert_tag :tag => 'td', :content => Regexp.new(a.first_name)

Railscasts - Free Ruby on Rails Screencasts