How can we execute Unit Tests against DOM manipulation?

后端 未结 3 1086
醉话见心
醉话见心 2021-02-13 06:43

The introduction to QUnit over at netTuts.com spawns an interesting exchange (never resolved) over how to apply unit tests against actions that manipulate the DOM. The following

相关标签:
3条回答
  • 2021-02-13 06:56

    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?

    0 讨论(0)
  • 2021-02-13 06:56

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

    0 讨论(0)
  • 2021-02-13 06:57

    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");
    
    0 讨论(0)
提交回复
热议问题