2016-04-22 42 views
7

Sono abbastanza nuovo in Javascript e ho una breve domanda riguardante la libreria Chai per fare test di unità.Qual è la differenza tra equal e eql in Chai Library

Quando stavo studiando alcuni materiali sulla libreria Chai, ho visto una dichiarazione che diceva uguale "Asserisce che il target è strettamente uguale (===) al valore" ed eql "Asserisce che il target è profondamente uguale al valore. ".

Ma sono confuso su quale sia la differenza tra strettamente e profondamente.

+0

Eventuali duplicati di [? Qual è la differenza tra pari ?, eql ?, ===, e ==] (http://stackoverflow.com/questions/7156955/whats-the-difference- tra-uguale-eql-e) – 8protons

risposta

9

Strictly uguale (o ===) significa che il vostro stanno confrontando esattamente lo stesso oggetto a se stesso:

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 

profondamente Pari d'altra parte significa che ogni proprietà del rispetto oggetti (e possibili oggetti collegati in profondità) hanno lo stesso valore. Quindi:

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