Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. module XMLMatchers
  2. require 'libxml'
  3. require 'rspec/expectations'
  4.  
  5. RSpec::Matchers.define :have_xml do |xpath, text|
  6. match do |body|
  7. parser = LibXML::XML::Parser.string body
  8. doc = parser.parse
  9. nodes = doc.find(xpath)
  10.  
  11. return false if nodes.empty?
  12. if text
  13. return false unless nodes.any? {|node| node.content == text}
  14. end
  15. return true
  16. end
  17.  
  18. failure_message_for_should do |body|
  19. "expected to find text '#{text}' in xml tag #{xpath} in:\n#{body}"
  20. end
  21.  
  22. failure_message_for_should_not do |response|
  23. "expected not to find text #{text} xml tag #{xpath} in:\n#{body}"
  24. end
  25.  
  26. description do
  27. "have xml tag #{xpath}"
  28. end
  29. end
  30. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement