简体   繁体   English

使用jQuery创建DOM结构进行单元测试

[英]Creating DOM structure using jQuery for unit testing

Suppose I have the following html: 假设我有以下html:

var testString = '
<span id='id'>
    <div>
        <table>
            <span id='anotherId' class='NODE customContext' name='2'></span>
        </table>
    </div>
</span>'

and I want to convert it to a DOM structure using jQuery (under the assumption that jQuery is the best option to handle this task) as my unit testing data to test the following code: 并且我想使用jQuery(假设jQuery是处理此任务的最佳选择)将其转换为DOM结构,作为我的单元测试数据来测试以下代码:

$('#' + ID + ' > div > table').each(function() {
    var span = $(this).find('span.' + NODE)[0];
    .
    .
    .
});

Is this the way to convert the testString to DOM structure? 这是将testString转换为DOM结构的方法吗?

var jQueryDOMElement = $(testString);

Or am I way off the mark, or is there a better way to do it? 还是我偏离标准,还是有更好的方法呢?

I just want to create a DOM structure without having to use createElement, elementNode... etc. I do not have access to test live, so I have to resort to using dummy test data/DOM structure. 我只想创建一个DOM结构,而不必使用createElement,elementNode ...等。我无权访问实时测试,因此我不得不诉诸使用虚拟测试数据/ DOM结构。

have you tried using JSTestDriver for testing your javascript. 您是否尝试过使用JSTestDriver测试您的JavaScript。 It allows you to inject DOM elements as you need it straight from the test without having to worry about how to do it where to do it. 它使您可以直接从测试中根据需要注入DOM元素,而不必担心如何在哪里进行操作。 The details are here . 详细信息在这里

I have started doing all my JS tests using this because it supports QUnit and YUI test for writting of tests. 我已经开始使用它进行所有的JS测试,因为它支持QUnit和YUI测试来编写测试。 It also got good support for CI servers 对CI服务器也有很好的支持

As far as I know, you must have a DOM element at first before you can create a complex structure (with a string). 据我所知,在创建复杂结构(带有字符串)之前,必须首先具有DOM元素。 You might create a hidden DIV for testing and then jq('#unit-test').replaceWith(testString). 您可以创建一个隐藏的DIV进行测试,然后创建jq('#unit-test')。replaceWith(testString)。

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

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