Created
June 10, 2014 21:52
-
-
Save soulcutter/d26de587e637983b51d8 to your computer and use it in GitHub Desktop.
General purpose RSpec 2.14 XML matcher
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
require 'nokogiri' | |
RSpec::Matchers.define :have_xml do |xpath, matcher| | |
match do |body| | |
doc = Nokogiri::XML::Document.parse(body) | |
nodes = Array(doc.xpath(xpath)) | |
nodes.map! { |node| node.respond_to?(:content) ? node.content : node } | |
if nodes.empty? | |
false | |
elsif matcher | |
nodes.all? { |node| matcher === node } | |
else | |
true | |
end | |
end | |
failure_message_for_should do |body| | |
"expected to find xml tag #{xpath} in:\n#{body}" | |
end | |
failure_message_for_should_not do |body| | |
"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