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

Issue

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?

Solution

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.

Answered By – manji

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published