运行角度测试时无法识别RxJs运算符

时间:2020-05-19 07:35:17

标签: javascript angular testing rxjs observable

因此,我有一个组件,可以在ngOnInit生命周期的表单控件之一中订阅值的更改。但是在编写针对它的测试时,我遇到了与跳过不是函数有关的错误。

我的组件文件如下

  ngOnInit() {
    this.buildForm();
    this.cronTextSubscription = this.cronForm.get('emailCron').valueChanges
      .skip(3)
      .debounceTime(3000)
      .distinctUntilChanged()
      .subscribe(cronPattern => {
        this.updateEmailCronPattern(cronPattern);
      });
    this.fetchCronExpression();
  }

运行测试时,出现以下错误。

 TypeError: this.cronForm.get(...).valueChanges.skip is not a function

我刚开始进行角度测试,因此我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果此代码在项目中有效,但在测试中出现问题,那么您需要验证测试this.cronForm.get('emailCron')是否返回正确的数据。

在这里您还使用了相当老的rxjs语法,请检查您的rxjs版本。也许是6(但是在这种情况下,项目也应该失败)。

然后您的代码应如下所示:

  ngOnInit() {
    this.buildForm();
    this.cronTextSubscription = this.cronForm.get('emailCron').valueChanges.pipe(
      skip(3),
      debounceTime(3000),
      distinctUntilChanged(),
    ).subscribe(cronPattern => {
      this.updateEmailCronPattern(cronPattern);
    });
    this.fetchCronExpression();
  }