单元测试出错:
Error: : handleError() method does not exist
Usage: spyOn(, )
at
at UserContext.apply (http://localhost:9876/karma_webpack/src/global-message/http-interceptors/http-error.interceptor.spec.ts:121:9)
at ZoneDelegate.invoke (http://localhost:9876/karma_webpack/C:/Code/SPA/spartacus/node_modules/zone.js/dist/zone.js:386:30)
at ProxyZoneSpec.onInvoke (http://localhost:9876/karma_webpack/C:/Code/SPA/spartacus/node_modules/zone.js/dist/zone-testing.js:292:43)
at ZoneDelegate.invoke (http://localhost:9876/karma_webpack/C:/Code/SPA/spartacus/node_modules/zone.js/dist/zone.js:385:36)
at Zone.run (http://localhost:9876/karma_webpack/C:/Code/SPA/spartacus/node_modules/zone.js/dist/zone.js:143:47)
at runInTestZone (http://localhost:9876/karma_webpack/C:/Code/SPA/spartacus/node_modules/zone.js/dist/zone-testing.js:545:38)
at UserContext. (http://localhost:9876/karma_webpack/C:/Code/SPA/spartacus/node_modules/zone.js/dist/zone-testing.js:560:24)
原因出在下图高亮的这行代码上:
handlerClass的类型是一个function,而不是具体的对象实例。因此,handler.handleError为undefined:
实际上,一个function可用的属性有且仅有下列几种:
使用下列代码后错误消失:
const handler = TestBed.inject(handlerClass) as HttpErrorHandler;
更多Jerry的原创文章,尽在:“汪子熙”: