【问题标题】:Jasmine: Testing static function in another classJasmine:在另一个类中测试静态函数
【发布时间】:2019-01-14 03:58:44
【问题描述】:

假设我有一个静态类和一个普通类,如下所示。

class StaticClass {
  static staticFunction() { 
    console.log('Static function called.');
  }
}

class NormalClass {
  normalFunction() { 
    StaticCLass.staticFunction();
  }
}

如何测试调用normalFunction()时是否调用了静态函数?

【问题讨论】:

    标签: testing static jasmine spy


    【解决方案1】:

    您可以像这样设置一个简单的间谍(正如您已经从问题中的标签猜到的那样):

    it('should test if the static function is being called ', () => {
      // Set up the spy on the static function in the StaticClass
      let spy = spyOn(StaticClass, 'staticFunction').and.callThrough();
      expect(spy).not.toHaveBeenCalled();
    
      // Trigger your function call
      component.normalFunction();
    
      // Verify the staticFunction has been called
      expect(spy).toHaveBeenCalled();
      expect(spy).toHaveBeenCalledTimes(1);
    });
    

    Here 是一个堆栈闪电战,已实现并通过了上述测试。

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 2011-05-03
      • 2020-10-09
      • 2019-05-19
      • 2016-05-27
      • 2018-09-15
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      相关资源
      最近更新 更多