assert_xpath

Hey guys, here’s a handy tip for testing XML: assert_xpath. I’m not sure where I found this.
Posted below or at: http://pastie.caboo.se/102945

# Add this to your test/test_helper.rb
class Test::Unit::TestCase
 
  def assert_xpath(xml, xpath)
    doc = REXML::Document.new(xml)
    assert REXML::XPath.first(doc, xpath),
      "expected xpath, but no xpath found matching #{xpath.inspect}
in:\n#{xml.inspect}"
  end
 
  def assert_xpath_value(xml, xpath, value)
    doc = REXML::Document.new(xml)
    assert node = REXML::XPath.first(doc, xpath),
      "expected xpath, but no xpath found matching #{xpath.inspect}
in:\n#{xml.inspect}"
    assert_equal value, node.text,
      "expected xpath #{xpath.inspect} to match #{value} but it was:
\n#{node.text}"
  end
 
end
Share:
  • del.icio.us
  • Reddit
  • Technorati
  • Twitter
  • Facebook
  • Google Bookmarks
  • HackerNews
  • PDF
  • RSS
This entry was posted in programming. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.