When you call new MyCoolObject()
you invoke the MyCoolObject
function and get a new object with the related prototype. This means that when you spyOn(MyCoolObject, "doSomething")
you're not setting up a spy on the object returned by the new
call, but on a possible doSomething
function on the MyCoolObject
function itself.
You should be able to do something like:
it("calls doSomething", function() {
var originalConstructor = MyCoolObject,
spiedObj;
spyOn(window, 'MyCoolObject').and.callFake(function() {
spiedObj = new originalConstructor();
spyOn(spiedObj, 'doSomething');
return spiedObj;
});
MyFunction();
expect(spiedObj.doSomething).toHaveBeenCalled();
});