I want to spy on a constructor function and tell how many times it's been called using jasmine. I'd usually do something like this to target a method of an object:
spyOn(lib,'methodName')
but in the case I'm trying to spy on the actualy constructor so I've tried:
spyOn(lib);
it('lib should be instantiated for each matching element', function () {
spyOn(lib);
expect(lib.calls.count()).toEqual(2);
});
Unfortunately this just gives me an error in the console:
"Error: undefined() method does not exist in ..."
how can I spy on the constructor?
I want to spy on a constructor function and tell how many times it's been called using jasmine. I'd usually do something like this to target a method of an object:
spyOn(lib,'methodName')
but in the case I'm trying to spy on the actualy constructor so I've tried:
spyOn(lib);
it('lib should be instantiated for each matching element', function () {
spyOn(lib);
expect(lib.calls.count()).toEqual(2);
});
Unfortunately this just gives me an error in the console:
"Error: undefined() method does not exist in ..."
how can I spy on the constructor?
Share Improve this question asked Sep 5, 2014 at 15:05 Mike RifginMike Rifgin 10.7k22 gold badges77 silver badges115 bronze badges 2 |1 Answer
Reset to default 17The spyOn()
function can only replace object properties, so the only thing you can do is spying on the prototype. Now if you would spy on the prototype of the real class, it would interfere with the other tests, so you have to use prototypal inheritance.
You can do something like this :
var mockClass = function (Subject) {
var Surrogate = function () {
Surrogate.prototype.constructor.apply(this, arguments);
};
Surrogate.prototype = Object.create(Subject.prototype);
Surrogate.prototype.constructor = Subject;
return Surrogate;
};
Some tests:
var My = function (a) {
this.init(a);
};
My.prototype = {
init: function (a) {
this.setA(a);
},
setA: function (a) {
this.a = a;
}
};
var Mock = mockClass(My);
spyOn(Mock.prototype, "constructor").andCallThrough();
spyOn(Mock.prototype, "init");
var m = new Mock(1);
expect(Mock.prototype.init).toBe(m.init);
expect(My.prototype.init).not.toBe(m.init);
expect(m.constructor).toHaveBeenCalledWith(1);
expect(m.init).toHaveBeenCalledWith(1);
expect(m.a).toBeUndefined();
m.setA(1);
expect(m.a).toBe(1);
spyOn(Mock.prototype, "setA").andCallFake(function (a) {
this.a = a + 1;
});
m.setA(1);
expect(m.setA).toHaveBeenCalledWith(1);
expect(m.a).toBe(2);
You cannot spy on the constructor
if your code uses an x.constructor
based type checking. But I think this can happen only by integration tests, and by badly designed codes...
spyOn(window, 'lib')
– javiyu Commented Sep 5, 2014 at 15:06