Jasmine has built-in matchers toBe
and toEqual
. If I have an object like this:
function Money(amount, currency){ this.amount = amount; this.currency = currency; this.sum = function (money){ return new Money(200, "USD"); } }
and try to compare new Money(200, "USD")
and the result of sum, these built-in matchers will not work as expected. I have managed to implement a work-around based on a custom equals
method and custom matcher, but it just seems to much work.
What is the standard way to compare objects in Jasmine?
You are trying to compare two different instances of an object which is true for regular equality ( a == b ) but not true for strict equality ( a === b). The comparator that jasmine uses is jasmine. Env. equals_() which looks for strict equality.
If the two objects have the same values, equals() will return true . In the second comparison, equals() checks to see whether the passed object is null, or if it's typed as a different class. If it's a different class then the objects are not equal.
ToEqual() ToEqual() is the simplest matcher present in the inbuilt library of Jasmine. It just matches whether the result of the operation given as an argument to this method matches with the result of it or not.
Use not. toEqual for check inequality of object. toEqual matches deep equality.
I was looking for the same thing and found an existing way to do so without any custom code or matchers. Use toEqual()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With