问题描述
同事们,谁能告诉我,我正在尝试为管道配置 testbed,并且只有当我将管道本身放入提供程序中时它才能工作,我将对其进行测试。 我无法以任何方式解决这个问题,你能告诉我这是怎么回事吗? 第二个问题:除了transform方法,pipe里面还有一个private方法,怎么测试??
解决方法
我会尝试以this的方式去做。
但是将其作为提供者很好,而且您正在掌握它。
要测试私有方法,您可以使环境适当,以便它遍历私有方法。
@Pipe({ name: 'xyz',pure: true })
export class SdDateFormatPipe implements PipeTransform {
value: number;
constructor(private datePipe: DatePipe,private configService: ConfigService) {}
transform(value: string): string {
if (value.includes('a')) {
this.setValue(1);
} else {
this.setValue(2);
}
}
private setValue(num: number) {
this.value = num;
}
}
it("should set value to 1 if the string has a",() => {
pipe.transform('abc');
expect(pipe.value).toBe(1);
});
it("should set value to 2 if the string does not have an a",() => {
pipe.transform('xyz');
expect(pipe.value).toBe(2);
});
所以在一天结束的时候,看看你从私有方法中得到了什么,并断言它做了它应该做的事情。
,如果没有依赖注入,您可以使用 let pipe = new SdDateFormatPipe() 实例化。
关于私有方法,它可能在公共方法中被调用,因此为了在您的测试中覆盖它,正确且推荐的方法是制作一个将调用私有方法的测试用例。