Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking object equality in Jasmine

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?

like image 925
Dan Avatar asked May 06 '13 14:05

Dan


People also ask

How do you 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.

How do you know if objects are equal?

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.

Which matter is used in jasmine to check whether the result is equal to true or false?

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.

How do you find not equal to in Jasmine?

Use not. toEqual for check inequality of object. toEqual matches deep equality.


1 Answers

I was looking for the same thing and found an existing way to do so without any custom code or matchers. Use toEqual().

like image 112
lukas.pukenis Avatar answered Oct 16 '22 11:10

lukas.pukenis