gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 01:39:32 28 4
gpt4 key购买 nike

我有一个 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

关于ruby - 如何使用 RSpec 测试 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20553024/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com