运行ng test
后,收到错误消息:
Error: Illegal state: Could not load the summary for directive AppComponent.
我不确定为什么我会收到此错误,因为我正在引用和声明AppComponent。
这是我的app.component.spec.ts
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { NO_ERRORS_SCHEMA } from "@angular/core";
import { Router } from "@angular/router";
import { ProjectService } from "../../shared/service/project/project.service";
import { AppComponent } from "./app.component";
describe("AppComponent", () => {
let comp: AppComponent;
let fixture: ComponentFixture<AppComponent>;
beforeEach(() => {
const routerStub = {
navigate: () => ({})
};
const projectServiceStub = {
GetActiveProject: () => ({
name: {}
})
};
TestBed.configureTestingModule({
declarations: [ AppComponent ],
schemas: [ NO_ERRORS_SCHEMA ],
providers: [
{ provide: Router, useValue: routerStub },
{ provide: ProjectService, useValue: projectServiceStub }
]
});
fixture = TestBed.createComponent(AppComponent);
comp = fixture.componentInstance;
});
it("can load instance", () => {
expect(comp).toBeTruthy();
});
describe("ngOnInit", () => {
it("makes expected calls", () => {
const routerStub: Router = fixture.debugElement.injector.get(Router);
const projectServiceStub: ProjectService = fixture.debugElement.injector.get(ProjectService);
spyOn(routerStub, "navigate");
spyOn(projectServiceStub, "GetActiveProject");
comp.ngOnInit();
expect(routerStub.navigate).toHaveBeenCalled();
expect(projectServiceStub.GetActiveProject).toHaveBeenCalled();
});
});
});
为什么会抛出此错误以及如何解决此错误?
编辑: 我使用Angular CLI的默认tsconfig和tsconfig.spec文件。 在此期间我升级到了Angular 6,我得到了sam错误。 即使用干净的配置文件创建一个新项目并替换src文件夹也无法解决错误。
答案 0 :(得分:0)
这些设置的组合冲突:
declarations: [ AppComponent ],
schemas: [ NO_ERRORS_SCHEMA ],
AppComponent引用了另一个组件,testbed模块无法找到该组件。无错误模式隐藏了AppComponent中的实际错误,但显示了AppComponent本身的错误,因为它无法正确加载。
要解决此问题,您可以确保引用每个子组件,或者导入整个模块:
import: [ AppModule ],