开发者

QUnit test fails: no result message given

开发者 https://www.devze.com 2023-03-20 23:23 出处:网络
Trying to get the hang of QUnit, running inte some trouble: When I run the following test: test(\"Trying QUnit\", function() {

Trying to get the hang of QUnit, running inte some trouble: When I run the following test:

   test("Trying QUnit", function() {
            expect(1);
            var div = $('<div>')
            div.addClass('field-box');
            same(fieldBox(), div, 'Expected ' + div + ' was: ' + fieldBox());
        });
    });

    function fieldBox() {
        return $('<div class="field-box">');
    }

I get the message:

Expected [object Object] was: [object Object]
Expected:   
[
  <div class="field-box"></div>
]

Which doesn't give me any hint of what is wrong. If I alter the fieldBox-method to return a div with class "field-boxing" instead, I get the following more explanatory message:

 Expected [object Object] was: [object Object]
Expected:   
[
  <div class="field-box"></div>
]
Result: 
[
  <div class="fi开发者_开发技巧eld-boxing"></div>
]
Diff:   
 [
   <div class="field-box"></div>
class="field-boxing"></div>
 ] 

Which leads me to believe that there was actually nothing wrong with my initial test, since no Diff was displayed. Yet it failed, why?


This test passes -

test("Trying QUnit", function() {
    expect(1);
    var div = $('<div>')
    div.addClass('field-box');
    same(fieldBox().html(), div.html(), 'Expected ' + div + ' was: ' + fieldBox());
});

Could it be that qunit is comparing two JavaScript objects, and although what is contained in the objects is the same the objects aren't the same (i.e. they don't point to the same object in memory) so the test fails.

EDIT

I dumped out the object graph for the two objects using the code below -

for (property in div[0]) {
    output += property + ': ' + div[0][property]+'; ';
}

the parentNode property for the 'var div' div is -

parentNode: null;

and for the 'fieldBox()' function div it's -

parentNode: [object DocumentFragment];

I presume this difference has occurred due to the different way the objects were created. Qunit will then spot this difference as it iterates through each objects properties and fail the test accordingly.


DOM objects are compared by their identity, not their content, even when using same (which is now deepEquals, same is deprecated).

With .html() you're comparing the text representation, there for actual content.

0

精彩评论

暂无评论...
验证码 换一张
取 消