4

我有一个功能

mutationToDom: function() {
    var container = document.createElement( 'mutation' );

    container.setAttribute( 'string', 'test' );

    return container;
}

容器是<mutation string="test"></mutation>

在单元测试中调用此函数后如何检查它?

QUnit.test( 'check function mutationToDom', function( assert ) {
    var container = testBlock.mutationToDom();
    assert.ok( container === ???, 'mutation is created with correct value' );
});

尝试#1

我尝试XMLSerializer()将 XML 转换为字符串

var containerString = new XMLSerializer().serializeToString(container);

但是,containerString

"<mutation xmlns="http://www.w3.org/1999/xhtml" string="test"></mutation>"

代替"<mutation string="test"></mutation>"

4

2 回答 2

2

检查一下怎么样outerHTML

var testBlock = {
  mutationToDom: function() {
    var container = document.createElement('mutation');
    container.setAttribute('string', 'test');
    return container;
  }
};

QUnit.test( 'check function mutationToDom', function( assert ) {
    var container = testBlock.mutationToDom();
    assert.ok( container.outerHTML === '<mutation string="test"></mutation>', 'mutation is created with correct value' );
});
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.6.0.css">
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.6.0.js"></script>

于 2018-05-07T04:17:14.413 回答
0

要回答标题中的特定问题,请尝试testObject instanceof Element...HTMLElement.outerHTML正如 31piy 所建议的,这可能是检查.

于 2018-06-06T22:27:37.280 回答