首页 > 解决方案 > Jasmine Karma 在角度 5 中调用登录组件

问题描述

我是 Jasmine 业力测试用例的新手。我尝试为登录组件编写业力测试用例。在那次点击中,提交按钮没有调用 component.ts 文件中的 onSubmit 方法

登录组件.ts

onSubmit() {
    this.authService.login(this.data.username, this.data.password)
      .delay(1000)
      .subscribe(data => {
        sessionStorage.setItem('token', JSON.stringify(data.token));
        if (this.urlDirect !== null) {
          window.location.href = this.sharedService.baseUrl + '/' + this.urlDirect;
        } else { this.router.navigate(['/dashboard']); }

      },
        error => {
          this.submitted = false;
          setInterval(() => {
            this.spinnerlogo = false;
          }, 1000);
          let i = 0;
          const timer = setInterval(() => {
            this.errorDiagnostic = 'Incorrect username or password.';
            i++;
            if (i === 10) {
              clearInterval(timer);
              this.spinnerlogo = false;
              this.errorDiagnostic = null;
            }
          }, 500);

        });

  }

login.component.spec.ts

it(`entering value in username and password input controls`, () => {
    userNameEl.nativeElement.value = 'admin';
    passwordEl.nativeElement.value = 'admin';
    fixture.detectChanges();
  });

  it('after entering value the button should enabled and click Action should happen', () => {
    submitEl.triggerEventHandler('click', null)
    // tslint:disable-next-line:no-unused-expression
    expect(comp.onSubmit).toHaveBeenCalled;
    // const loginButtonSpy = spyOn(comp, 'onSubmit');
    // submitEl.triggerEventHandler('click', null);
    // expect(loginButtonSpy).toHaveBeenCalled();
  });

标签: unit-testingangular5karma-jasmine

解决方案


推荐阅读