Angular Юнит-тесты с использованием Karma, ошибка с веткой test или serviceSpy () - PullRequest
0 голосов
/ 10 января 2020

В настоящее время я тестирую приложение angular в Angular 7, но я не могу понять, как протестировать эту ветвь функции отправки, у меня примерно 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);
  });

1 Ответ

0 голосов
/ 14 января 2020

Изменил l oop с while на for, в angular 8 этот l oop больше не будет нужен.

...