gpt4 book ai didi

testing - Jasmine:在另一个类中测试静态函数

转载 作者:行者123 更新时间:2023-11-28 20:36:02 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com