相关疑难解决方法(0)

使用jasmine进行subscribe方法的angular2测试

我有一个规范代码来测试这样

 it('login test', () => {

      const fixture = TestBed.createComponent(component);
      fixture.detectChanges();
      let authService = fixture.debugElement.injector.get(Auth);
      spyOn(authService, 'login').and.returnValue('');

      const elements = fixture.nativeElement;
      fixture.componentInstance.login();
      expect(authService.login).toHaveBeenCalled();
    });
Run Code Online (Sandbox Code Playgroud)

和这样的实现代码

login() {

    this.auth.login(this.username, this.password).subscribe(() => {

      }
    });
  }
Run Code Online (Sandbox Code Playgroud)

它给出了错误:

this.auth.login(...).subscribe不是一个函数

为什么会发生这种错误?

unit-testing karma-jasmine angular

15
推荐指数
3
解决办法
3万
查看次数

标签 统计

angular ×1

karma-jasmine ×1

unit-testing ×1