Created
July 26, 2016 08:13
-
-
Save haslinger/f716ad49724ea4e2dcbeef4f88932518 to your computer and use it in GitHub Desktop.
have_xml matcher Rspec 3 compatible
This file contains hidden or 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
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