在混合应用程序中,尝试测试具有 Angular 已降级依赖项的 AngularJS 服务

Within hybrid app, trying to test AngularJS service which has Angular dependency that was downgraded

我有一个 AngularJS 服务,它的依赖项已转换为 .ts Angular 服务。在我的主入口点文件中,我这样做了:

angular
  .module('MyNg1Module')
  .factory('MyDowngradedService', downgradeInjectable(MyFancyModernAngularService))

虽然这在实际的应用程序中效果很好,但当 运行对该服务进行测试(业力规范)时,我在尝试将 MyDowngradedService 单例注入时遇到了未知的提供程序错误AngularJS...经过一些搜索我发现:https://angular.io/api/upgrade/static/testing/createAngularJSTestingModule,所以在我测试的入口点文件中,我做了:

import AppModule from 'entrypoints/main';
import { createAngularJSTestingModule } from '@angular/upgrade/static/testing';

beforeEach(angular.mock.module(createAngularJSTestingModule([AppModule])));

然后在规范中,我添加了:

 beforeEach(angular.mock.module('MyNg1Module'))

相信这会让 AngularJS 注入器访问降级的模块...但是,当我 运行 规范时,我得到:

Error while instantiating injectable 'MyDowngradedService': Need to call TestBed.initTestEnvironment() first

那些 angular 文档中没有关于 TestBed.initTestEnvironment()

的内容

原来我需要做:

import { destroyPlatform } from '@angular/core';
import 'zone.js/dist/zone';
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import { createAngularJSTestingModule } from '@angular/upgrade/static/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting, } from '@angular/platform-browser-dynamic/testing';

beforeEach(() => {
  destroyPlatform();
  getTestBed().initTestEnvironment(
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting()
  );
});