Karma unit testing error: Unexpected value imported by the module. Please add a @NgModule annotation
Karma unit testing error: Unexpected value imported by the module. Please add a @NgModule annotation
我通过以下方式创建了一个全新的组件:
ng g mytest1
然后我将构造函数行更改为:
constructor(private dialogRef: MatDialogRef<Mytest1Component>) { }
,并添加了所需的导入:
import { MatDialogRef } from '@angular/material';
之后我 运行 Karma 单元测试项目通过:
ng test
测试失败。我收到此错误消息:
Error: StaticInjectorError(DynamicTestModule)[Mytest1Component -> MatDialogRef]: StaticInjectorError(Platform: core)[Mytest1Component -> MatDialogRef]: NullInjectorError: No provider for MatDialogRef!
为了解决这个问题,我在 beforeEach 部分添加了 Import 语句:
import { MatDialogRef } from '@angular/material';
//...
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ Mytest1Component ],
imports: [MatDialogRef],
})
.compileComponents();
}));
现在我遇到了这个新错误,我无法修复它:
Failed: Unexpected value 'MatDialogRef' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
谁能说明我应该在哪里添加 @NgModule 注释,或者我是否做错了什么?
谢谢。
您在组件中注入 MatDialogRef
:
constructor(private dialogRef: MatDialogRef<Mytest1Component>) { }
因此 testBed 期望将与 provider
相同的内容注入到 TestBed。或者你也可以给它提供一个MockDialogueService
。
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ Mytest1Component ],
providers: [ MatDialogRef ],
})
.compileComponents();
}));
使用:
imports: [MatDialogModule],
改为
我通过以下方式创建了一个全新的组件:
ng g mytest1
然后我将构造函数行更改为:
constructor(private dialogRef: MatDialogRef<Mytest1Component>) { }
,并添加了所需的导入:
import { MatDialogRef } from '@angular/material';
之后我 运行 Karma 单元测试项目通过:
ng test
测试失败。我收到此错误消息:
Error: StaticInjectorError(DynamicTestModule)[Mytest1Component -> MatDialogRef]: StaticInjectorError(Platform: core)[Mytest1Component -> MatDialogRef]: NullInjectorError: No provider for MatDialogRef!
为了解决这个问题,我在 beforeEach 部分添加了 Import 语句:
import { MatDialogRef } from '@angular/material';
//...
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ Mytest1Component ],
imports: [MatDialogRef],
})
.compileComponents();
}));
现在我遇到了这个新错误,我无法修复它:
Failed: Unexpected value 'MatDialogRef' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
谁能说明我应该在哪里添加 @NgModule 注释,或者我是否做错了什么?
谢谢。
您在组件中注入 MatDialogRef
:
constructor(private dialogRef: MatDialogRef<Mytest1Component>) { }
因此 testBed 期望将与 provider
相同的内容注入到 TestBed。或者你也可以给它提供一个MockDialogueService
。
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ Mytest1Component ],
providers: [ MatDialogRef ],
})
.compileComponents();
}));
使用:
imports: [MatDialogModule],
改为