我想对使用辅助类的角度组件进行单元测试。辅助类及其函数不应成为此测试的一部分,而应被模拟。该组件可能如下所示:
import { MyHelperClass } from "./my-helper-class";
export class MyComponent {
public doStuff() {
const helper = new MyHelperClass();
if (helper.check()) {
// code I want to test
}
}
}
我想将功能helper.check()
从单元测试中排除,并假设它返回true
(或在第二次测试中返回错误)。所以我希望我的测试看起来像这样:
it("#doStuff should do something, assuming helper.check() is true, () => {
// make constructor of MyHelperClass return a Mock
// (or somehow spy on helper.check() and return true?)
expect(component.doStuff()).toBe(someValue);
});