假设我有一个静态类和一个普通类,如下所示。
class StaticClass {
static staticFunction() {
console.log('Static function called.');
}
}
class NormalClass {
normalFunction() {
StaticCLass.staticFunction();
}
}
如何测试在调用 normalFunction()
时是否调用了静态函数?
最佳答案
您可以像这样设置一个简单的 spy (正如您已经从问题中的标签猜到的那样):
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是实现并通过上述测试的 stackblitz。
关于testing - Jasmine:在另一个类中测试静态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54175618/