Jasmine: testing toHaveBeenCalledWith (params), where params is an object

I am testing Jasmine, having a weird result when looking for expected parameters as a result of a spy.

I am testing the toHaveBeenCalledWith () method and looking for something like this:

{
  foo: bar,
  thing: [1,2,3],
  etc: function() { blah; }
}

It fails, but the error message seems to confirm that the same object is indeed found.

Any reasons why this could be so?

+3
source share
1 answer

The definitions of equivalent functions are not equal. In this way,

function() { return 'blah'; } == function() { return 'blah'; } // returns false

toHaveBeenCalledWith() Jasmine, , spied object. , (, , ), toHaveBeenCalledWith()? .

Jasmine, , , . :

describe("A spy example", function() {
    var baz  = {
        foo: 'bar',
        thing: [1,2,3],
        etc: function() { }
    };

    beforeEach(function() {
        spyOn(baz, 'etc');
        baz.etc(baz);
    });

    it("succeeds", function() {
        expect(baz.etc).toHaveBeenCalledWith(baz);
    });

    it("fails", function() {
        expect(baz.etc).toHaveBeenCalledWith({
            foo: 'bar',
            thing: [1,2,3],
            etc: function() { }
        });
    });
});
+5

All Articles