0

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

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

这些角度文档中没有关于TestBed.initTestEnvironment()

4

1 回答 1

0

原来我需要做:

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()
  );
});
于 2022-01-13T06:02:51.607 回答