首页 > 解决方案 > Angular 5 单元测试路由器导航

问题描述

我想测试这个功能。当返回 true 我想在这个路由器中导航。

我的组件代码:

  onLogin() {
    this.loading = true;
    this.auth.login(
      this.loginForm.controls['username'].value,
      this.loginForm.controls['password'].value)
      .subscribe(
      result => {
        if (result === true) {
          this.router.navigate(['/home']);
        } else {
          this.loading = false;
        }
      }
      );
  }

我试过这个测试,结果是真的

  it('should call service.login when onLogin', done => {
    const mock = [];
    spyOn(component['auth'], 'login').and.callThrough()
    component['auth'].login('abcs', '1').subscribe(
      result => {
        if (result === true) {
        }
        console.log(result)
          done();
      });
    component.onLogin();
  })

标签: angularunit-testingtypescripttestingjasmine

解决方案


推荐阅读