How can we execute Unit Tests against DOM manipulation?

谁都会走 提交于 2019-12-03 10:35:06
Charles Anderson

The latest version of QUnit supports a #qunit-fixture element that lets you add HTML to the QUnit web page.

E.g., in your HTML:

<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>

and in your JavaScript:

$('<input id="ResultTestBox" type="text"/>').appendTo('#qunit-fixture');
var result = add(a, b);

equals(result, $('input#ResultTestBox').val(), "testing result box value");
Steve Greatrex

Surely what you actually care about is that the val method gets called on the return value of $(“input#ResultTestBox”)—you don't need to test the functionality of the jQuery method itself. Why not inject a mock implementation of the jQuery object and test against that?

Sudhir Jonathan

You can run them in env.js, if you don't want the hassle of creating html page sandboxes for all your tests.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!