本文介绍了angular 4 单元测试错误`TypeError: ctor is not a constructor`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试测试我的路由解析器,在测试时我得到了 TypeError: ctor is not a constructor
并且不知道为什么会在打字稿编译时没有错误时发生.
I am trying to test my route resolver and while testing I got TypeError: ctor is not a constructor
and no idea why it happen while typescript compile time no error.
TypeError: ctor is not a constructor
TypeError: ctor is not a constructor
at _createClass (http://localhost:9877/_karma_webpack_/vendor.bundle.js:42355:26)
at _createProviderInstance$1 (http://localhost:9877/_karma_webpack_/vendor.bundle.js:42330:26)
at resolveNgModuleDep (http://localhost:9877/_karma_webpack_/vendor.bundle.js:42315:17)
at _createClass (http://localhost:9877/_karma_webpack_/vendor.bundle.js:42362:26)
at _createProviderInstance$1 (http://localhost:9877/_karma_webpack_/vendor.bundle.js:42330:26)
at resolveNgModuleDep (http://localhost:9877/_karma_webpack_/vendor.bundle.js:42315:17)
at NgModuleRef_.webpackJsonp../node_modules/@angular/core/@angular/core.es5.js.NgModuleRef_.get (http://localhost:9877/_karma_webpack_/vendor.bundle.js:43401:16)
at TestBed.webpackJsonp../node_modules/@angular/core/@angular/core/testing.es5.js.TestBed.get (http://localhost:9877/_karma_webpack_/vendor.bundle.js:48412:47)
at http://localhost:9877/_karma_webpack_/vendor.bundle.js:48418:61
at Array.map (native)
推荐答案
这可能是提供程序声明中的错误.
This can be an error in the providers declarations.
当您尝试模拟提供者并使用 useClass 而不是 useValue 时,会触发错误TypeError: ctor is not a constructor".
When you try to mock a provider and use useClass instead of useValue the error "TypeError: ctor is not a constructor" is fired.
这是一个触发错误的例子:
Here is an example that fires the error :
providers: [{provide: OrderService, useClass: new OrderServiceMock()}]
正确的声明是:
providers: [{provide: OrderService, useValue: new OrderServiceMock()}]
这篇关于angular 4 单元测试错误`TypeError: ctor is not a constructor`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!