Skip to content

Instantly share code, notes, and snippets.

@haslinger
Created July 26, 2016 08:13
Show Gist options
  • Save haslinger/f716ad49724ea4e2dcbeef4f88932518 to your computer and use it in GitHub Desktop.
Save haslinger/f716ad49724ea4e2dcbeef4f88932518 to your computer and use it in GitHub Desktop.
have_xml matcher Rspec 3 compatible
module XMLMatchers
require 'libxml'
require 'rspec/expectations'
RSpec::Matchers.define :have_xml do |xpath, text|
match do |body|
parser = LibXML::XML::Parser.string body
doc = parser.parse
nodes = doc.find(xpath)
return false if nodes.empty?
if text
return false unless nodes.any? {|node| node.content == text}
end
return true
end
failure_message_for_should do |body|
"expected to find text '#{text}' in xml tag #{xpath} in:\n#{body}"
end
failure_message_for_should_not do |response|
"expected not to find text #{text} xml tag #{xpath} in:\n#{body}"
end
description do
"have xml tag #{xpath}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment