Angular +(Jasmine/Karma) - 错误:非法状态:无法加载指令的摘要

SrA*_*Axi 6 jasmine karma-runner angular

我真的想解决这个错误,互联网上没有任何东西可以帮助我...

lr-categories.component.spec.ts:

export function main() {
  describe('LrCategoriesComponent', () => {
    let fixture: ComponentFixture<LrCategoriesComponent>;
    let component: LrCategoriesComponent;
    let de: DebugElement;
    let glService: GeneralLedgeService;
    let lrService: LrService;
    let spy: jasmine.Spy;

    beforeEach(async(() => {
      TestBed.configureTestingModule({
        declarations: [
          LrComponent,
          LrMappingsComponent,
          LrCategoriesComponent,
        ],
        imports: [
          CommonModule,
          SharedModule,
          LrRoutingModule,
          AgGridModule.withComponents(
            [
              ButtonComponent,
              ColumnHeaderComponent,
              TypeaheadEditorComponent,
              ButtonGroupComponent
            ]
          )
        ],
        providers: [
          LrService,
          { provide: GeneralLedgeService, useClass: MockGeneralLedgeService },
          CompleterService,
        ]
      }).compileComponents().then(() => {
        // initialization
        fixture = TestBed.createComponent(LrCategoriesComponent);
        component = fixture.componentInstance;
        de = fixture.debugElement;

        // Service getters
        glService = de.injector.get(GeneralLedgeService);
        lrService = de.injector.get(LrService);
      });
    }));

    // beforeEach(() => {
    //   // initialization
    //   fixture = TestBed.createComponent(LrCategoriesComponent);
    //   component = fixture.componentInstance;
    //   de = fixture.debugElement;
    //
    //   // Service getters
    //   glService = de.injector.get(GeneralLedgeService);
    //   lrService = de.injector.get(LrService);
    // });

    it('should create LrCategoriesComponent', (() => {
      expect(component).toBeDefined();
    }));
  });
}
Run Code Online (Sandbox Code Playgroud)

错误: 在此输入图像描述

错误:'未处理的承诺拒绝:','非法状态:无法加载指令LrCategoriesComponent的摘要.'

任何建议都被接受!我在这一点上绝望!

SrA*_*Axi 3

我找到了一个解决方案,我将引导您完成它(解决方案是第 3 点)

1.我将初始化内容从.compileComponents().then(() => {...});移至beforeEach(() => {...});(可以看到有人评论了)

export function main() {
  describe('LrCategoriesComponent', () => {
    let fixture: ComponentFixture<LrCategoriesComponent>;
    let component: LrCategoriesComponent;
    let de: DebugElement;
    let glService: GeneralLedgeService;
    let lrService: LrService;
    let spy: jasmine.Spy;

    beforeEach(async(() => {
      TestBed.configureTestingModule({
        // Module's stuff here
      }).compileComponents();
    }));

    beforeEach(() => {
      // initialization
      fixture = TestBed.createComponent(LrCategoriesComponent);
      component = fixture.componentInstance;
      de = fixture.debugElement;

      // Service getters
      glService = de.injector.get(GeneralLedgeService);
      lrService = de.injector.get(LrService);
    });

    it('should create LrCategoriesComponent', (() => {
      expect(component).toBeDefined();
    }));
  });
}
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

错误:此测试模块使用组件 ToolBarComponent,该组件使用“templateUrl”或“styleUrls”,但它们从未被编译。

此错误是因为 中声明的组件之一SharedModule在编译时出现问题(实际上, 中的任何组件都会发生这种情况declarations[]SharedModule

因此,我开始寻找解决导入(或其他重型模块)时规范文件设置的方法SharedModule

2.我在这里找到了一种新方法:

const oldResetTestingModule = TestBed.resetTestingModule;
    beforeAll(done => (async () => {
      TestBed.resetTestingModule();
      TestBed.configureTestingModule({
       // Module's stuff here
      });
      await TestBed.compileComponents();

      // prevent Angular from resetting testing module
      TestBed.resetTestingModule = () => TestBed;
    })().then(done).catch(done.fail));

    afterAll(() => {
      // reinstate resetTestingModule method
      TestBed.resetTestingModule = oldResetTestingModule;
      TestBed.resetTestingModule();
    });
Run Code Online (Sandbox Code Playgroud)

尽管它最终以惊人的速度完成了所有工作(5 秒与之前的 20 秒相比),但这种方法有一个主要缺点:依赖项是每次创建一次describe(),而不是每次测试创建一次。这意味着您继承了前一个测试用例的状态。我们不想要那样!

3.我回到第一点,尝试理解并应用更智能的异步逻辑......:

- 解决方案 -

beforeEach(done => (async () => {
      TestBed.configureTestingModule({
        // Module's stuff here, including SharedModule
      });
      await TestBed.compileComponents();
    })().then(done).catch(done.fail));

    beforeEach(() => {
      // initialization
      fixture = TestBed.createComponent(LrCategoriesComponent);
      component = fixture.componentInstance;
      de = fixture.debugElement;

      // Service getters
      glService = de.injector.get(GeneralLedgeService);
      lrService = de.injector.get(LrService);
    });
Run Code Online (Sandbox Code Playgroud)

通过使用,async / await我确保在尝试编译Component之前正确配置了Module

有了这个,我成功地编译了该组件,并为每个测试用例场景提供了一个干净且新鲜的实例来测试!