0

我已经使用 Angular 升级设置了 Angular Hybrid 设置以实现性能方法,并且能够毫无问题地运行应用程序。

但是,我在运行 Angular 端测试用例时遇到了问题,我提供了组件、规范和所有相关的片段。任何人都可以帮助我解决可能是什么问题以及如何解决它。

如果需要更多详细信息,请告诉我

角组件:componentA.ts

constructor(@Inject('$rootScope') public rootScope) { }

Angular 组件规范:componentA.spec.ts

TestBed.configureTestingModule({
  declarations: [ ComponentA ],
  providers: [
    {
      provide: '$rootScope',
      useFactory: ($injector: any) => $injector.get('$rootScope'),
      deps: ['$injector']
    }
  ],
  imports: [
    createAngularTestingModule([ 'app'])
  ]
})

错误

Error: [$injector:unpr] Unknown provider: $rootElementProvider <- $rootElement <- $location <- $location
    at eval (webpack:///./node_modules/angular/angular.js?:68:12)
    at eval (webpack:///./node_modules/angular/angular.js?:4418:19)
    at Object.getService [as get] (webpack:///./node_modules/angular/angular.js?:4571:39)
    at eval (webpack:///./node_modules/angular/angular.js?:4423:45)
    at getService (webpack:///./node_modules/angular/angular.js?:4571:39)
    at injectionArgs (webpack:///./node_modules/angular/angular.js?:4595:58)
    at Object.invoke (webpack:///./node_modules/angular/angular.js?:4617:18)
    at eval (webpack:///./node_modules/angular/angular.js?:4424:37)
    at getService (webpack:///./node_modules/angular/angular.js?:4571:39)
    at injectionArgs (webpack:///./node_modules/angular/angular.js?:4595:58)
NullInjectorError: R3InjectorError(DynamicTestModule)[$rootScope -> $rootScope]: 
  NullInjectorError: No provider for $rootScope!
error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ '$rootScope', '$rootScope' ] })
    at NullInjector.get (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:1085:1)
    at R3Injector.get (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:16984:1)
    at R3Injector.get (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:16984:1)
    at NgModuleRef$1.get (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:36608:1)
    at Object.get (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:34252:1)
    at getOrCreateInjectable (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:5892:1)
    at ɵɵdirectiveInject (http://localhost:9876/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:21251:1)
4

1 回答 1

2

就像错误说你缺少$rootElement提供者一样。您可以通过在TestBed.configureTestingModule执行之前插入以下内容来实现。

angular.module(<your_angularjs_module>).provider({
   $rootElement: {
       $get: () => {
           return angular.element(document.getElementById(<your_root_angularjs_element_id>));
       }
   }
});
于 2020-08-06T11:15:01.913 回答