首页 > 解决方案 > 使用“ng test”升级 Angular v9 后无法设置基本提供程序错误

问题描述

从 v8 升级到 v9 后,我的所有单元测试都已更新并通过。但是,现在我收到以下错误:

Cannot set base providers because it has already been called

研究表明,我在申请中曾打过电话BrowserDynamicTestingModuleplatformBrowserDynamicTesting()打过两次电话。我搜索了我的整个应用程序,但在我的test.ts文件中只找到了一次:

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

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

declare const require: {
  context(
    path: string,
    deep?: boolean,
    filter?: RegExp
  ): {
    keys(): string[];
    <T>(id: string): T;
  };
};

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

我不确定错误可能来自哪里。

标签: angularunit-testingangular-cli

解决方案


推荐阅读