【发布时间】:2020-01-10 14:18:15
【问题描述】:
我目前正在 Angular 7 中测试一个 Angular 应用程序,但我不知道如何测试提交功能的这个分支,我在这个组件中有大约 20 多个测试,其中一些甚至使用相同的服务间谍作为不起作用的测试:
分支:
submit(): void {
if (this.systemTable) {
// this.formArray = this.formBuilder.array([]);
while (this.formArray.length) {
this.formArray.removeAt(0);
console.log('dfdfdf');
} //this.formArray.removeAt(0);
} //...
我的测试:
it('submit() should remove when systemTable is true', () => {
app.ngOnInit();
app.systemTable = true; //Entering the branch i wan't to test
const f = app.formArray;
app.addRow(); //This function adds a row into the app.formRow, I checked it's values with a debugger and it's working
const userServiceSpy = spyOn(app.formArray, 'removeAt');
app.submit();
expect(userServiceSpy).toHaveBeenCalledTimes(1);
});
【问题讨论】:
-
您是否多次使用同一个间谍将其推向更高的范围。我主要在 beforeEach 中定义它们。喜欢:
let spy: any;,然后是beforeEach(() => { spy = spyOn() })。这是评论而不是答案,因为您没有明确的问题,因此无法给出明确的答案。
标签: angular unit-testing karma-jasmine karma-runner