简体   繁体   English

使用 RSpec 和 rails 测试“HTML 固定装置”

[英]Testing “HTML fixtures” with RSpec and rails

I have a web scraper built to parse html from a website and I'm trying to write tests for it.我有一个 web 刮板,用于从网站解析 html,我正在尝试为它编写测试。

The class I'm trying to test receives a Nokogiri HTML object and extracts the required data from it.我正在尝试测试的 class 接收到 Nokogiri HTML object 并从中提取所需的数据。 Now as usual the html can vary, sometimes elements will be missing or whatnot.现在像往常一样 html 可能会有所不同,有时元素会丢失或诸如此类。 I need to test these different situations.我需要测试这些不同的情况。

So what I'd like to do is make a bunch of html files, each one representing a case with a particular element missing etc. For each html file, I wish to also construct an associated hash of the data I would expect the scraper to extract, assuming it is working correctly.所以我想做的是制作一堆 html 文件,每个文件代表一个缺少特定元素的案例等。对于每个 html 文件,我还希望构建一个关联的 Z0800FC577294C34E0B28AD28394 数据提取,假设它工作正常。

So I would like to write a test which will iterate over these html files and compare the data extracted by the class being tested against the expected data and report whether or not it is correct.因此,我想编写一个测试,它将遍历这些 html 文件,并将正在测试的 class 提取的数据与预期数据进行比较,并报告它是否正确。

Any suggestions as to how to do this?关于如何做到这一点的任何建议?

Have a look at the Artifice , fakeweb or webmock gems, which override net/http in order to supply testable results.看看Artificefakewebwebmock gem,它们会覆盖 net/http 以提供可测试的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM