【问题标题】:Angular2 / ng test ERROR: 'Unhandled Promise rejection'Angular2 / ng 测试错误:'未处理的承诺拒绝'
【发布时间】:2017-02-23 10:02:29
【问题描述】:

我目前正在使用 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 节点:6.7.0 操作系统: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)

【问题讨论】:

  • 您找到解决方案了吗?我也有同样的问题。

标签: angular testing


【解决方案1】:

我找到了解决问题的方法。我将测试中 appModule 的导入替换为所需的所有模块、组件和服务。我认为有一个错误,因为 appmodule 也导入了路由器模块。

beforeEach(() => TestBed.configureTestingModule({
    declarations: [component1, component2, ...],
    imports: [module1, module2, ...],
    providers: [service1, service2, { provide: Router, useValue: fakeRouter }]
 }));

【讨论】:

    猜你喜欢
    • 2017-10-21
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多