javascript - 错误:模块“DynamicTestModule”导入了意外的值“DomSanitizer”。请添加@NgModule 注释
问题描述
我尝试测试 DomSanitizer 导入。
但我仍然收到此错误:
Error: Unexpected value 'DomSanitizer' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.
我的测试用例如下所示:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, DomSanitizer, DossierFileService, ErrorProcessor, MatDialog, BrowserModule],
declarations: [ DossierPersonalDataComponent ],
providers: [{
provide: DomSanitizer,
useValue: {
sanitize: () =>'safeString',
bypassSecurityTrustHtml: () => 'safeString'
}
}]
})
.compileComponents();
}));
那么我必须改变什么?
谢谢
这是 ts 文件中的 de 依赖项:
constructor(
private dossierService: DossierService,
private route: ActivatedRoute,
private sanitizer: DomSanitizer,
private dossierFileService: DossierFileService,
private errorProcessor: ErrorProcessor,
private dialog: MatDialog
)
解决方案
DomSanitizer
从中删除imports
。您应该只导入模块,而不是服务。你应该提供服务。如果DossierFileService
,ErrorProcessor
是服务(我不确定 MatDialog 是否),请将它们移至提供者。
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, MatDialog, BrowserModule],
declarations: [ DossierPersonalDataComponent ],
providers: [{
provide: DomSanitizer,
useValue: {
sanitize: () =>'safeString',
bypassSecurityTrustHtml: () => 'safeString'
}
},
DossierFileService,
ErrorProcessor,
]
})
.compileComponents();
}));
推荐阅读
- oracle - Oracle Data Modeler 中的对象权限
- python - 如何在多索引中找到索引列?
- python - 将我的 Django docker 容器链接到 postgres 容器
- .net-core - 测试不在 Azure Devops 编译的 aspnet core 3.0 测试项目上运行
- c# - AssertConfigurationIsValid() 使用嵌套的 ConstructUsing 时,不忽略私有 setter
- javascript - 如何将登录凭据从 PHP Apache 服务器发送到 Node.js 服务器
- amazon-web-services - Powershell 无法删除我刚刚写的文件
- python - 在 Pypy 上使用 pip 安装包的问题
- c# - Windows 窗体中 Treeview 中的 Aftercheck 和 AfterSelect 事件
- php - 对 axios 请求使用动态参数