业力:已经实例化测试模块时无法配置测试模块

问题描述 投票:1回答:1

这里是我的测试:

fdescribe('ValueService', () => {

  it('#getValue should return real value', () => {
    expect(true).toBeTruthy();
   });
});

我有这个错误:

失败:已经实例化测试模块时,无法配置测试模块。确保在inject之前没有使用R3TestBed.configureTestingModule。 错误:已经实例化测试模块时,无法配置测试模块。确保在inject之前没有使用R3TestBed.configureTestingModule。 在TestBedRender3.prototype.assertNotInstantiated(http:/ localhost:9876 / _karma_webpack_ / vendor.js:76150:13) 在TestBedRender3.prototype.configureTestingModule(http:/ localhost:9876 / _karma_webpack_ / vendor.js:76045:9) 在TestBedRender3.configureTestingModule(http:/ localhost:9876 / _karma_webpack_ / vendor.js:75933:9) 在匿名函数中(http:/ localhost:9876 / _karma_webpack_ / main.js:27942:5) 在ZoneDelegate.prototype.invoke(http:/ localhost:9876 / _karma_webpack_ / polyfills.js:13991:17) 在AsyncTestZoneSpec.prototype.onInvoke(http:/ localhost:9876 / _karma_webpack_ / vendor.js:124156:21) 在ProxyZoneSpec.prototype.onInvoke(http:/ localhost:9876 / _karma_webpack_ / vendor.js:125618:17) 在ZoneDelegate.prototype.invoke(http:/ localhost:9876 / _karma_webpack_ / polyfills.js:13991:17) 在Zone.prototype.runGuarded(http:/ localhost:9876 / _karma_webpack_ / polyfills.js:13761:25) 在runInTestZone(http:/ localhost:9876 / _karma_webpack_ / vendor.js:124278:13) NullInjectorError:R3InjectorError(DynamicTestModule)[IRechercheService-> IRechercheService]: NullInjectorError:没有提供IRechercheService的提供程序! 错误属性:Object({ngTempTokenPath:null,ngTokenPath:['IRechercheService','IRechercheService']}) 在 在NullInjector.prototype.get(http:/ localhost:9876 / _karma_webpack_ / vendor.js:42155:13) 在R3Injector.prototype.get(http:/ localhost:9876 / _karma_webpack_ / vendor.js:52781:13) 在R3Injector.prototype.get(http:/ localhost:9876 / _karma_webpack_ / vendor.js:52781:13) 在NgModuleRef $ 1.prototype.get(http:/ localhost:9876 / _karma_webpack_ / vendor.js:66043:9) 到达时(http:/ localhost:9876 / _karma_webpack_ / vendor.js:64279:13) 在getOrCreateInjectable(http:/ localhost:9876 / _karma_webpack_ / vendor.js:45064:17) 在ɵɵdirectiveInject(http:/ localhost:9876 / _karma_webpack_ / vendor.js:55585:5) 在IdentificationRqComponent_Factory(功能代码:3:3) 在getNodeInjectable(http:/ localhost:9876 / _karma_webpack_ / vendor.js:45172:13) 在InstantiateRootComponent(http:/ localhost:9876 / _karma_webpack_ / vendor.js:49487:5) 失败:已经实例化测试模块时,无法配置测试模块。确保在inject之前没有使用R3TestBed.configureTestingModule。 错误:已经实例化测试模块时,无法配置测试模块。确保在inject之前没有使用R3TestBed.configureTestingModule。 在TestBedRender3.prototype.assertNotInstantiated ##标题##(http:/ localhost:9876 / _karma_webpack_ / vendor.js:76150:13) 在TestBedRender3.prototype.configureTestingModule(http:/ localhost:9876 / _karma_webpack_ / vendor.js:76045:9) 在TestBedRender3.configureTestingModule(http:/ localhost:9876 / _karma_webpack_ / vendor.js:75933:9) 在匿名函数中(http:/ localhost:9876 / _karma_webpack_ / main.js:95084:5) 在ZoneDelegate.prototype.invoke(http:/ localhost:9876 / _karma_webpack_ / polyfills.js:13991:17) 在AsyncTestZoneSpec.prototype.onInvoke(http:/ localhost:9876 / _karma_webpack_ / vendor.js:124156:21) 在ProxyZoneSpec.prototype.onInvoke(http:/ localhost:9876 / _karma_webpack_ / vendor.js:125618:17) 在ZoneDelegate.prototype.invoke(http:/ localhost:9876 / _karma_webpack_ / polyfills.js:13991:17) 在Zone.prototype.runGuarded(http:/ localhost:9876 / _karma_webpack_ / polyfills.js:13761:25) 在runInTestZone(http:/ localhost:9876 / _karma_webpack_ / vendor.js:124278:13) NullInjectorError:R3InjectorError(DynamicTestModule)[IDroitsObligationsService-> IDroitsObligationsService]: NullInjectorError:没有IDroitsObligationsService的提供者! 错误属性:Object({ngTempTokenPath:null,ngTokenPath:['IDroitsObligationsService','IDroitsObligationsService']}) 在 在NullInjector.prototype.get(http:/ localhost:9876 / _karma_webpack_ / vendor.js:42155:13) 在R3Injector.prototype.get(http:/ localhost:9876 / _karma_webpack_ / vendor.js:52781:13) 在R3Injector.prototype.get(http:/ localhost:9876 / _karma_webpack_ / vendor.js:52781:13) 在NgModuleRef $ 1.prototype.get(http:/ localhost:9876 / _karma_webpack_ / vendor.js:66043:9) 到达时(http:/ localhost:9876 / _karma_webpack_ / vendor.js:64279:13) 在getOrCreateInjectable(http:/ localhost:9876 / _karma_webpack_ / vendor.js:45064:17) 在ɵɵdirectiveInject(http:/ localhost:9876 / _karma_webpack_ / vendor.js:55585:5) 在DroitsObligationsComponent_Factory(功能代码:3:3) 在getNodeInjectable(http:/ localhost:9876 / _karma_webpack_ / vendor.js:45172:13) 在InstantiateRootComponent(http:/ localhost:9876 / _karma_webpack_ / vendor.js:49487:5)

这里是我的依赖项:

“ devDependencies”:{ “ @ angular-devkit / build-angular”:“〜0.803.26”, “ @ angular / cli”:“〜9.1.6”, “ @ angular / compiler-cli”:“〜9.1.9”, “ @ angular / language-service”:“〜9.1.9”, “ @ types / datatables.net-buttons”:“〜1.4.0”, “ @ types /茉莉花”:“ 2.8.3”, “ @ types / jasminewd2”:“〜2.0.5”, “ @ types / node”:“〜7.10.11”, “ @ typescript-eslint / eslint-plugin”:“〜2.34.0”, “ @ typescript-eslint / parser”:“〜2.34.0”, “ azure-devops-node-api”:“ ^ 9.0.1”, “ codelyzer”:“〜4.3.0”, “ config”:“〜2.0.1”, “ copyfiles”:“ ^ 2.1.1”, “ current-git-branch”:“ ^ 1.1.0”, “ eslint”:“〜7.0.0”, “ eslint-plugin-react”:“〜7.20.0”, “茉莉花芯”:“〜3.5.0”, “ jasmine-spec-reporter”:“〜5.0.2”, “业力”:“〜5.0.9”, “业力铬发射器”:“〜3.1.0”, “业力报废的伊斯坦布尔记者”:“〜3.0.2”, “ karma-ie-launcher”:“〜1.0.0”, “ karma-jasmine”:“〜3.1.1”, “ karma-jasmine-html-reporter”:“〜1.5.4”, “ karma-junit-reporter”:“〜2.0.1”, “ ng-mocks”:“〜9.3.0”, “提示”:“ ^ 1.0.0”, “量角器”:“〜7.0.0”, “ ts-node”:“〜4.1.0”, “ typescript”:“〜3.7.5” }

angular testing karma-runner
1个回答
0
投票

正如与作者讨论的那样,当具有两个或更多规范文件时,在每个测试床外先初始化TestBed时会出现问题。

例如:

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));
describe('AppComponent', () => {
  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
 });

将在每次测试前实例化一个TestBed,而不仅是规格文件。因此,如果您还有一个带有TestBed和beforeEach的.spec,它将被解释为2个TestBed,如下所示:

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));
describe('AppComponent', () => {
  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
 });

错误

失败:当测试模块具有以下功能时,无法配置测试模块 已经实例化。

将是正确的,因为您实例化了两个TestBed(但在两个规格文件中)。

要解决此问题,必须始终将TestBed定义(即beforeEach)放在这样的描述中:

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.