问题描述
我正在编写一个javascript单元测试套件,我想添加的一个功能是断言某个元素及其子元素与给定的HTML结构匹配。
I'm writing a javascript unit test suite and one of the features I'd like to add is the ability to assert that a certain element and its children match a given HTML structure.
我的第一个想法是使用jQuery(好吧,Sizzle)并要求用户写声明做出断言。我的第一个问题是之前是否已经完成过这项工作?我能偷走它吗?。如果没有,是否有任何规范打印出如何解析Zen Code语句?鉴于Sizzle的力量,我能做出任何捷径吗?
My first idea is to use jQuery (well, Sizzle) and ask that users write Zen Code statements to make assertions. My first question is "Has this been done before? Can I steal it?". If not, is there a specification printed anywhere for how to parse a Zen Code statement? Are there any shortcuts I could make, given the power of Sizzle?
推荐答案
我认为你可以用嘶嘶声+ http://api.jquery.com/size/
I think you can get there with sizzle + http://api.jquery.com/size/
示例zen代码查询是div#page> div.logo + ul#navigation> li * 5> a。测试一个页面在jQuery中是否具有相同的结构就像$(div#page> div.logo + ul#navigation> li> a)。size()== 5。
The example zen code query is "div#page>div.logo+ul#navigation>li*5>a". Testing if a page has that same structure in jQuery would be as easy as $("div#page > div.logo + ul#navigation > li > a").size() == 5.
除非你的用户已经熟悉zen代码,否则使用像assertSelects(selector,number_of_returned_items)这样的api的测试应该更加温和。
Unless your users are already familiar with zen code, tests with an api like assertSelects(selector, number_of_returned_items) should be cozier.
这篇关于反向禅编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!