【发布时间】:2023-03-12 11:51:01
【问题描述】:
我通过以下方式创建了一个全新的组件:
ng g mytest1
然后我将构造函数行改为:
constructor(private dialogRef: MatDialogRef<Mytest1Component>) { }
,并添加了所需的导入:
import { MatDialogRef } from '@angular/material';
之后我通过以下方式运行 Karma 单元测试项目:
ng test
测试失败。我收到此错误消息:
错误:StaticInjectorError(DynamicTestModule)[Mytest1Component -> MatDialogRef]: StaticInjectorError(Platform: core)[Mytest1Component -> MatDialogRef]: NullInjectorError: 没有 MatDialogRef 的提供者!
为了解决这个问题,我在 beforeEach 部分添加了 Import 语句:
import { MatDialogRef } from '@angular/material';
//...
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ Mytest1Component ],
imports: [MatDialogRef],
})
.compileComponents();
}));
现在我遇到了这个新错误,我无法修复:
失败:模块“DynamicTestModule”导入了意外的值“MatDialogRef”。请添加@NgModule 注解。
有人可以澄清我应该在哪里添加@NgModule 注释,或者我做错了什么吗?
谢谢。
【问题讨论】:
标签: angular unit-testing electron karma-jasmine ng-modules