Skip to content

Instantly share code, notes, and snippets.

@soulcutter
Created June 10, 2014 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soulcutter/d26de587e637983b51d8 to your computer and use it in GitHub Desktop.
Save soulcutter/d26de587e637983b51d8 to your computer and use it in GitHub Desktop.
General purpose RSpec 2.14 XML matcher
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