Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Jasmine单元测试:TypeError:this.sitesTable.renderRows不是函数_Angular - Fatal编程技术网

Angular Jasmine单元测试:TypeError:this.sitesTable.renderRows不是函数

Angular Jasmine单元测试:TypeError:this.sitesTable.renderRows不是函数,angular,Angular,在Angular/Jasmine/Karma的简单单元测试中,我有以下错误 TypeError: this.sitesTable.renderRows is not a function 我在表格里有一张垫子桌子。我想测试我重置表单和renderRows的方法,但它抛出了这个错误。它在正常的“非测试环境”下工作 测试: 测试方法: resetForm(){ this.studyForm.resetForm(); this.ngOnInit(); this.sitesTa

在Angular/Jasmine/Karma的简单单元测试中,我有以下错误

TypeError: this.sitesTable.renderRows is not a function
我在表格里有一张垫子桌子。我想测试我重置表单和renderRows的方法,但它抛出了这个错误。它在正常的“非测试环境”下工作

测试:

测试方法:

resetForm(){
    this.studyForm.resetForm();
    this.ngOnInit();
    this.sitesTable.renderRows();
  }
我可以请你们帮助我,或者给我文章的链接,或者任何有帮助的东西吗


谢谢

尝试在测试中引用新MatTable并设置为component.sitesTable。 可能是ViewChild没有在测试中检索MatTable实例。这将是一个简单的解决方案。然后,您可以使用renderRows方法监视


祝你好运

我找到了解决方案,我认为它对有相同问题的人很有用

解决方案:将MaterialModule和BrowserAnimationsModule添加到测试文件中的导入

解决方案说明:

我阅读了关于测试床的文档 . 如果我理解正确,TestBed.configureTestingModule()创建模块的完全分离版本。 所以在我的例子中,我有两个模块:AppModule和TestBed(类似于AppModuleTest)

在这种情况下,应在两个模块中添加MaterialModule。 还应导入BrowserAnimationsModule以启用MaterialModule

如果有更有经验的人能纠正我或补充一些东西,我会非常感激

resetForm(){
    this.studyForm.resetForm();
    this.ngOnInit();
    this.sitesTable.renderRows();
  }