2016-04-22 16 views
7

Jestem całkiem nowym użytkownikiem Javascript i mam krótkie pytanie dotyczące biblioteki Chai do wykonywania testów jednostkowych.Jaka jest różnica między równymi i eql w Bibliotece Chai

Kiedy studiowałem niektóre materiały w bibliotece Chai, zobaczyłem oświadczenie mówiące: "Zapewnia, że ​​cel jest ściśle równy (===), aby wycenić" i eql "Zapewnia, że ​​cel jest głęboko równy wartości. ".

Ale jestem zdezorientowany tym, co różnica między ściśle i głęboko.

+0

Możliwy duplikat [? Jaka jest różnica między równy ?, eql ?, === i ==] (http://stackoverflow.com/questions/7156955/whats-the-difference- między-równym-eql-i) – 8protons

Odpowiedz

9

Ściśle równe (lub ===) oznacza, że ​​porównujesz dokładnie takie same obiekt do siebie:

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 

Głęboko Równe z drugiej strony oznacza, że ​​każdy nieruchomość porównywanego obiekty (i możliwe obiekty głęboko połączone) mają tę samą wartość. Więc:

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 
Powiązane problemy