测试时未定义角度区域onMicroTaskEmpty

时间:2018-02-06 15:40:27

标签: angular

我有以下testBed:

TestBed.configureTestingModule({
  imports: [ReactiveFormsModule, MaterialModules, RouterTestingModule],
  declarations: [SigninComponent, LoaderComponent],
  providers: [
    MatSnackBar, {
      provide: Router, useClass: RouterTestingModule
    }, {
      provide: ActivatedRoute, useValue: {
        snapshot: { params: Observable.of({ email: 'test@bizanalyst.in' }) }
      }
    }, {
      provide: NgZone, useValue: mockNgZone
    },
    LoaderService,
    FormBuilder,
    StoreOperationService
  ]
}).compileComponents()

此测试会抛出错误:

it('should have a defined component', () => {
  expect(component).toBeNull(false)
})

错误是:

TypeError: Cannot read property 'subscribe' of undefined
    at new ApplicationRef_ (webpack:///node_modules/@angular/core/@angular/core.es5.js:4684:0 <- config/spec-bundle.js:5351:37)
    at _createClass (webpack:///node_modules/@angular/core/@angular/core.es5.js:9539:0 <- config/spec-bundle.js:10206:20)
    at _createProviderInstance$1 (webpack:///node_modules/@angular/core/@angular/core.es5.js:9503:0 <- config/spec-bundle.js:10170:26)
    at initNgModule (webpack:///node_modules/@angular/core/@angular/core.es5.js:9456:0 <- config/spec-bundle.js:10123:28)

该行对应于ApplicationRef函数内的错误:

 _this._zone.onMicrotaskEmpty.subscribe({ next: function () { _this._zone.run(function () { _this.tick(); }); } });

1 个答案:

答案 0 :(得分:0)

只需从测试模块依赖项中删除provide: NgZone, useValue: mockNgZone。无需在茉莉花中使用它。只需在提供商列表中跳过NgZone的声明即可。