Skip to content

Instantly share code, notes, and snippets.

@schustafa
Created March 3, 2011 15:30
Show Gist options
  • Save schustafa/852940 to your computer and use it in GitHub Desktop.
Save schustafa/852940 to your computer and use it in GitHub Desktop.
# Credit: http://johnleach.co.uk/words/585/testing-xml-with-rspec-xpath-and-libxml
require 'libxml'
RSpec::Matchers.define :have_xml do |xpath, text|
match do |body|
parser = LibXML::XML::Parser.string body
doc = parser.parse
nodes = doc.find(xpath)
nodes.empty?.should be_false
if text
nodes.each do |node|
node.content.should == text
end
end
true
end
failure_message_for_should do |body|
"expected to find xml tag #{xpath} in:\n#{body}"
end
failure_message_for_should_not do |response|
"expected not to find xml tag #{xpath} in:\n#{body}"
end
description do
"have xml tag #{xpath}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment