ruby – 如何使用RSpec测试XML文件?

我有一个RSS提要,我正在编写一个RSpec测试.我想测试 XML文档是否具有正确的节点和结构.不幸的是,我没有找到任何很好的例子,如何以干净的方式做到这一点.我只找到一些半实施的解决方案和过时的博文.如何使用RSpec测试XML文档的结构?

解决方法

嗨,我可以建议你使用自定义匹配器.
require 'nokogiri' 
    RSpec::Matchers.define :have_xml do |xpath,text|   
      match do |body|
        doc = Nokogiri::XML::Document.parse(body)
        nodes = doc.xpath(xpath)
        nodes.empty?.should be_false
        if text
          nodes.each do |node|
            node.content.should == text
          end
        end
        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

完整的例子可以在这里找到
https://gist.github.com/Fivell/8025849

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...