How to use Jasmine spies on an object created inside another method?

前端 未结 2 1068
[愿得一人]
[愿得一人] 2021-02-01 06:43

Given the following code snippet, how would you create a Jasmine spyOn test to confirm that doSomething gets called when you run MyFunction

2条回答
  •  感动是毒
    2021-02-01 07:48

    Alternatively, as Gregg hinted, we could work with 'prototype'. That is, instead of spying on MyCoolObject directly, we can spy on MyCoolObject.prototype.

    describe("MyFunction", function () {
        it("calls doSomething", function () {
            spyOn(MyCoolObject.prototype, "doSomething");
            MyFunction();
            expect(MyCoolObject.prototype.doSomething).toHaveBeenCalled();
    
        });
    });
    

提交回复
热议问题