Created
March 3, 2011 15:30
-
-
Save schustafa/852940 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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