DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Using Xpath To Test The Rails

06.18.2005
| 6477 views |
  • submit to reddit
        Originally posted in #rubyonrails by ? springjp

def test_account_list
    #  There should be some accounts to test
    assert @ttrueheart.accounts.count > 0
 
    @request.session[:user] = @ttrueheart
    get :list
    assert_success
 
    xml = nil
    assert_nothing_thrown { xml = REXML::Document.new(@response.body) }
 
    @ttrueheart.accounts.each do |account|
      td = REXML::XPath.match(xml, "//td[text()=\"#{account.display_string}\"]")
      assert_equal(1, td.size)  # Unique entry
 
      assert_equal(3, REXML::XPath.match(td, 'following-sibling::td/a').size)
 
      href = REXML::XPath.match(td, "following-sibling::td/a[@href='/account/activity_list/#{account.id}']")
      assert_equal(1, href.size)
 
      href = REXML::XPath.match(td, "following-sibling::td/a[@href='/account/edit/#{account.id}']")
      assert_equal(1, href.size)
 
      href = REXML::XPath.match(td, "following-sibling::td/a[@href='/account/delete/#{account.id}']")
      assert_equal(1, href.size)
    end
  end