MrP*_*mer 3 karma-jasmine electron ng-modules angular
我通过以下方式创建了一个新的新组件:
ng g mytest1
Run Code Online (Sandbox Code Playgroud)
然后,我将构造函数行更改为:
constructor(private dialogRef: MatDialogRef<Mytest1Component>) { }
Run Code Online (Sandbox Code Playgroud)
,并添加了所需的导入:
import { MatDialogRef } from '@angular/material';
Run Code Online (Sandbox Code Playgroud)
之后,我通过以下方式运行了业力单元测试项目:
ng test
Run Code Online (Sandbox Code Playgroud)
测试失败。我收到此错误消息:
错误:StaticInjectorError(DynamicTestModule)[Mytest1Component-> MatDialogRef]:StaticInjectorError(平台:core)[Mytest1Component-> MatDialogRef]:NullInjectorError:MatDialogRef没有提供者!
为了解决这个问题,我在beforeEach部分中添加了Import语句:
import { MatDialogRef } from '@angular/material';
//...
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ Mytest1Component ],
imports: [MatDialogRef],
})
.compileComponents();
}));
Run Code Online (Sandbox Code Playgroud)
现在,我收到了这个新错误,无法修复:
失败:模块“ DynamicTestModule”导入了意外的值“ MatDialogRef”。请添加一个@NgModule批注。
有人可以弄清楚应该在哪里添加@NgModule批注,或者我做错了什么吗?
谢谢。
您正在组件中注入MatDialogRef:
constructor(private dialogRef: MatDialogRef<Mytest1Component>) { }
Run Code Online (Sandbox Code Playgroud)
因此,testBed希望注入provider与TestBed 相同的对象。或者,您也可以提供一个MockDialogueService。
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ Mytest1Component ],
providers: [ MatDialogRef ],
})
.compileComponents();
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6268 次 |
| 最近记录: |