Jasmine - Spying on a method call within a constructor

前端 未结 2 1635
情歌与酒
情歌与酒 2020-12-04 10:15

I want to test whether the following method is called with in my Javascript object constructor. From what I have seen in the Jasmine documentation, I can spy on a constructo

2条回答
  •  情歌与酒
    2020-12-04 10:55

    Spy directly on the prototype method:

    describe("The Klass constructor", function() {
      it("should call its prototype's called_method", function() {
          spyOn(Klass.prototype, 'called_method');  //.andCallThrough();
          var k = new Klass();
          expect(Klass.prototype.called_method).toHaveBeenCalled();
      });
    });
    

提交回复
热议问题