我真的在努力解决这个错误,目前互联网上没有任何帮助...
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();
}));
});
}
ERROR: 'Unhandled Promise rejection:', 'Illegal state: Could not load the summary for directive LrCategoriesComponent.'
接受任何建议!我现在很绝望!
最佳答案
我找到了一个解决方案,我将向您介绍它(解决方案是第 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();
}));
});
}
这给了我以下错误:
Error: This test module uses the component ToolBarComponent which is using a "templateUrl" or "styleUrls", but they were never compiled.
此错误是因为在 SharedModule
中声明的组件之一编译时出现问题(实际上,这发生在 declarations[]
的 SharedModule
中的任何组件)。
因此,我开始寻找解决导入时规范文件设置的方法 SharedModule
(或其他重型模块)。
<强>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
在尝试编译组件 之前,我确保模块 已正确配置。
有了这个,我设法编译了组件,并拥有了一个全新的实例来测试每个测试用例场景!
关于Angular + (Jasmine/Karma) - 错误 : Illegal state: Could not load the summary for directive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48151314/