Skip to content

Instantly share code, notes, and snippets.

@pxlpnk
Last active December 26, 2015 22:29
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 pxlpnk/7223848 to your computer and use it in GitHub Desktop.
Save pxlpnk/7223848 to your computer and use it in GitHub Desktop.
rexml xpath matcher for rspec
# cheap mans xpath matcher
# inspired by: https://gist.github.com/faun/4279964
RSpec::Matchers.define :have_xml do |xpath, text|
match do |body|
doc = REXML::Document.new(body)
nodes = REXML::XPath.match(doc, xpath)
if text
nodes.each do |node|
node.content.should == text
end
end
nodes.empty? ? false : 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