gpt4 book ai didi

angular - 如何对 void 方法进行单元测试

转载 作者:太空狗 更新时间:2023-10-29 17:09:45 24 4
gpt4 key购买 nike

我正在努力达到更多的代码覆盖率。我有一个只触发通知的“信息”方法,不需要响应。如何对其进行单元测试?

public error(message?: any, ...optionalParams: any[]) {
if (this.isErrorEnabled()) {
console.error(`${this.name}: ${message}`, ...optionalParams);
}
}

最佳答案

您可以使用 spies 测试它的副作用,例如:

describe('error method', => {
it('should log an error on the console', () => {
spyOn(console, 'error');

error(...);

expect(console.error).toHaveBeenCalledWith(...);
});

...
});

关于angular - 如何对 void 方法进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40611567/

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