angular - Jasmine 无法识别 Angular 的 DomSanitizer
问题描述
我正在使用 Jasmine 对 Angular 5 应用程序进行单元测试。我到了需要测试一个依赖于的函数的地步DomSanitizer
:
loadImage() {
this.image = this.sanitizer.bypassSecurityTrustStyle(`url(${this.assetUrl})`);
}
我验证了这个函数可以完美运行,这意味着它DomSanitizer
已经在构造函数中并且它的语法是正确的。
我的单元测试如下:
it('loads the Image', () => {
component.loadImage()
fixture.detectChanges();
expect(component.imageUrl).toBe('...');
});
此外,DomSanitizer
是的一部分TestBed
providers
:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
CommonModule
],
declarations: [
MyComponent
],
providers: [
DomSanitizer
],
schemas: [ NO_ERRORS_SCHEMA ]
})
.compileComponents();
}));
然而,Jasmine 在对函数进行单元测试时抛出了这个错误:
ERROR: 'Unhandled Promise rejection:', '_this.sanitizer.bypassSecurityTrustStyle is not a function', '; Zone:', 'angular', '; Task:', 'Promise.then', '; Value:', TypeError: _this.sanitizer.bypassSecurityTrustStyle is not a function
有任何想法吗?
谢谢!
解决方案
将其更改为 BrowserModule 而不是 CommonModule 并且还可以省略 DomSanitizer:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
BrowserModule
],
declarations: [
MyComponent
]
schemas: [ NO_ERRORS_SCHEMA ]
})
.compileComponents();
}));
推荐阅读
- python - 使用 Python 生成动态 sql 查询
- android - 如何在我的 Razorpay 帐户上激活 BharatQR?我正在使用测试模式帐户
- python - 如何在 admin.py 中为搜索字段添加占位符
- amazon-cloudformation - AWS CDK 管道错误 - 找不到与“xxxxx”匹配的堆栈
- python - 如何为 MNIST 时尚建立一个 CNN 模型并用另一组来自网络的图像对其进行测试?
- html - 使图像滑块响应
- android - 在 Android 中将 Fragment 作为 Activity 调用
- laravel - 使用 FCM 发送通知
- android - 没有获取正确的文件 URI,而是获取内容 URI
- azure-functions - 使用 C# 在 azure (windows) 函数上添加/安装 java?