Angular(Jasmine / Karma) – 错误:非法状态:无法加载指令的摘
我真的想解决这个错误,互联网上没有任何东西可以帮助我…
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(); })); }); } 错误:
任何建议都被接受!我在这一点上绝望!
我找到了一个解决方案,我将引导您完成它(解决方案是第3点):
1.我从.compileComponents()移动了初始化内容.然后(()=> {…}); to 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(); })); }); } 这给了我以下错误:
此错误是因为在SharedModule中声明的其中一个组件在编译时遇到了问题(实际上,这发生在SharedModule声明[]中的任何组件上). 因此,我开始寻找在导入SharedModule(或其他重型模块)时解决spec文件设置的方法. 2.我发现了一种新方法here: 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(); }); 即使它最终以惊人的速度(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); }); 通过使用async / await来确保在尝试编译Component之前正确配置了Module. 有了这个,我设法编译组件并有一个干净,新鲜的实例来测试每个测试用例场景! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |