2016-04-22 5 views
7

私はJavascriptには新しく、単体テストを行うためのチャイライブラリーに関して簡単な質問があります。チャイライブラリーでequalとeqlの違いは何ですか

私はChai図書館でいくつかの資料を勉強していましたが、「ターゲットは厳密に(===)valueと等しいと主張する」というステートメントを見ましたが、eqlはターゲットがvalueと深く等しいと主張します。 "

しかし、私はその違いが厳密で深いのか混乱しています。

var myObj = { 
    testProperty: 'testValue' 
}; 
var anotherReference = myObj; 

expect(myObj).to.equal(anotherReference); // The same object, only referenced by another variable 
expect(myObj).to.not.equal({testProperty: 'testValue'}); // Even though it has the same property and value, it is not exactly the same object 

深く均等一方のあらゆるを比較することを意味:

+0

[?等しいとの違いは何?EQL?===、および==]の可能な重複(http://stackoverflow.com/questions/7156955/whats-the-difference- between-equal-eql-and) – 8protons

答えて

9

厳密(又は===)あなたはを正確自体に同じオブジェクトが比較されていることに等しい意味しますオブジェクト(およびディープリンクオブジェクト)は同じ値を持ちます。だから、:

var myObject = { 
    testProperty: 'testValue', 
    deepObj: { 
     deepTestProperty: 'deepTestValue' 
    } 
} 
var anotherObject = { 
    testProperty: 'testValue', 
    deepObj: { 
     deepTestProperty: 'deepTestValue' 
    } 
} 
var myOtherReference = myObject; 

expect(myObject).to.eql(anotherObject); // is true as all properties are the same, even the inner object (deep) one 
expect(myObject).to.eql(myOtherReference) // is still also true for the same reason 
関連する問題