我正在尝试为组件编写单元测试。由于我有@ViewChild元素,我想使用TestComponentBuilder
,
it('should close modal',
async(inject([TestComponentBuilder], (tcb) => {
tcb.createAsync(UploadImage).then((fixture: ComponentFixture<UploadImage>) => {
fixture.detectChanges();
let instance: UploadImage = fixture.componentInstance;
spyOn(instance.uploadDialog.nativeElement, 'close');
instance.closeModal();
expect(instance.uploadDialog.nativeElement.close).toHaveBeenCalled();
})
}))
);
然而,在我写fixture.detectChanges()
时,在tcb测试中,无论后面写的是什么,我都会收到以下错误消息。问题是错误消息根本没有写入,只有堆栈跟踪。
invoke@/Users/username/project-folder/config/spec-bundle.js:8059:35 <- webpack:///~/zone.js/dist/zone.js:323:0
run@/Users/username/project-folder/config/spec-bundle.js:7952:51 <- webpack:///~/zone.js/dist/zone.js:216:0
/Users/username/project-folder/config/spec-bundle.js:8307:62 <- webpack:///~/zone.js/dist/zone.js:571:0
invokeTask@/Users/username/project-folder/config/spec-bundle.js:8092:44 <- webpack:///~/zone.js/dist/zone.js:356:0
runTask@/Users/username/project-folder/config/spec-bundle.js:7992:59 <- webpack:///~/zone.js/dist/zone.js:256:0
drainMicroTaskQueue@/Users/username/project-folder/config/spec-bundle.js:8210:44 <- webpack:///~/zone.js/dist/zone.js:474:0
run@/Users/username/project-folder/config/spec-bundle.js:4193:30 <- webpack:///~/core-js/modules/es6.promise.js:89:0
/Users/username/project-folder/config/spec-bundle.js:4206:32 <- webpack:///~/core-js/modules/es6.promise.js:102:0
flush@/Users/username/project-folder/config/spec-bundle.js:4551:12 <- webpack:///~/core-js/modules/_microtask.js:18:0
有趣的是,我不使用TestComponentBuilder
的测试运行良好。如果有人遇到同样的问题,请帮助解决问题。