在
netTuts.com的QUnit over的介绍产生了一个有趣的交换(从未解决)如何对操纵DOM的操作应用单元测试.以下引用(Alex York)得到了关键:
What would be nice is that if you had
a function like this:function add(a,b) { var result = a +
b;
$(“input#ResultTestBox”).val(result);In the above test,I would love to
test two things: the addition of a and
b,and the result correctly being put
into a DOM element. I would love to
test the second thing by providing
some mock HTML. Possible?
但是,就像我说的…没有解决.可分解?
解决方法
最新版本的QUnit支持#qunit-fixture元素,可以将HTML添加到QUnit网页.
例如,在你的HTML中:
<ol id="qunit-tests"></ol> <div id="qunit-fixture">test markup,will be hidden</div>
并在您的JavaScript中:
$('<input id="ResultTestBox" type="text"/>').appendTo('#qunit-fixture'); var result = add(a,b); equals(result,$('input#ResultTestBox').val(),"testing result Box value");