Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine spy expects to be called with "Object(...)"

I am completing a migration from Jasmine 1.3 to 2.0. So far I have refactored most of the code to comply with 2.0's newer syntax. However, a certain kind of tests are still failing.

In short, my test looks like this:

var obj = new CustomCriteria();

spyOn(my, "function");
my.function(obj);
expect(my.function).toHaveBeenCalledWith({ big: "fat object" });

my CustomCriteria class:

var CustomCriteria = function() {
    this.big = "fat object";
};

The test fails with the following:

Expected spy function to have been called with [ Object({ big: "fat object" }) ] but actual calls were [ ({ big: "fat object" }) ].

Note how the expectation has an "Object" wrapping around it, but the second does not. This test did not fail in < 2.0 of Jasmine, but is now failing after I update Jasmine. How can I fix this?

Update: I tinkered around with creating a new object via newing a function vs. object literal syntax, and it appears that the __proto__s are different. Perhaps this affects Jasmine's equality comparison?

like image 957
geoff Avatar asked Jul 23 '15 22:07

geoff


1 Answers

Prior to version 2, objects are equal if they have the same properties and values (see v1.3.1 code)

From version 2 onward, object constructors are also compared (see v2.0 code).

In your case: CustomCriteria and {} do not have the same constructor.

P.S.: The exception message also changed to contain the constructor name in it.

like image 54
manji Avatar answered Sep 27 '22 19:09

manji