Angular2 / ng test错误:“未处理的Promise拒绝”

4

我目前在使用angular-cli开发angular2项目。从明天开始,我在运行ng test命令时遇到了一个新的错误,下面是错误的详细信息。每次运行ng test命令都会触发此错误。我已经找到了导致错误的spec.ts文件中的代码:

const fakeRouter = jasmine.createSpyObj('Router', ['navigate']);
beforeEach(() => TestBed.configureTestingModule({
    imports: [AppModule, RouterTestingModule],
    providers: [{ provide: Router, useValue: fakeRouter }]
 }));

当我移除供应商路由器时,错误消失了。然而,所有的测试都成功通过了。
我的angular-cli版本:
angular-cli: 1.0.0-beta.32.3 node: 6.7.0 os: win32 x64
错误信息:
ERROR: 'Unhandled Promise rejection:', 'Cannot set property 'afterPreactivation' of undefined', '; Zone:', 'ProxyZone', '; Task:', 'Promise.then', '; Value:', TypeError{}, 'TypeError: Cannot set property 'afterPreactivation' of undefined
        at http://localhost:9876/base/src/test.ts?10abbba6756b062e1f8ba4c1873d1f257bc67f65:66907:49
        at ZoneDelegate.invoke (http://localhost:9876/base/src/test.ts?10abbba6756b062e1f8ba4c1873d1f257bc67f65:94256:26)
        at ProxyZoneSpec.onInvoke (http://localhost:9876/base/src/test.ts?10abbba6756b062e1f8ba4c1873d1f257bc67f65:70192:39)
        at ZoneDelegate.invoke (http://localhost:9876/base/src/test.ts?10abbba6756b062e1f8ba4c1873d1f257bc67f65:94255:32)
        at Zone.run (http://localhost:9876/base/src/test.ts?10abbba6756b062e1f8ba4c1873d1f257bc67f65:94052:43)
        at http://localhost:9876/base/src/test.ts?10abbba6756b062e1f8ba4c1873d1f257bc67f65:94605:57
        at ZoneDelegate.invokeTask (http://localhost:9876/base/src/test.ts?10abbba6756b062e1f8ba4c1873d1f257bc67f65:94289:31)
        at ProxyZoneSpec.onInvokeTask (http://localhost:9876/base/src/test.ts?10abbba6756b062e1f8ba4c1873d1f257bc67f65:70216:39)
        at ZoneDelegate.invokeTask (http://localhost:9876/base/src/test.ts?10abbba6756b062e1f8ba4c1873d1f257bc67f65:94288:36)
        at Zone.runTask (http://localhost:9876/base/src/test.ts?10abbba6756b062e1f8ba4c1873d1f257bc67f65:94092:47)
        at drainMicroTaskQueue (http://localhost:9876/base/src/test.ts?10abbba6756b062e1f8ba4c1873d1f257bc67f65:94455:35)

你找到解决方案了吗?我也遇到了同样的问题。 - Zolcsak Tamas
1个回答

1
我找到了解决问题的方法。我用所有需要的模块、组件和服务替换了我的测试中appModule的导入。我认为这是因为appmodule也导入了路由器模块所以出现了错误。
beforeEach(() => TestBed.configureTestingModule({
    declarations: [component1, component2, ...],
    imports: [module1, module2, ...],
    providers: [service1, service2, { provide: Router, useValue: fakeRouter }]
 }));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接