【发布时间】:2021-12-07 05:28:58
【问题描述】:
我在测试我的组件时遇到问题。主要问题是当我运行fixture.detectChanges() 时。 当我不触发它时,所有测试都通过,没有任何问题。 这是测试:
describe("simple HTML", () => {
// beforeEach(() => {
// fixture.detectChanges();
// });
it('should be created', () => {
expect(component).toBeTruthy();
});
it("Component should render html h3 tag with title Icons & Measuerements", () => {
const h3Ele = fixture.debugElement.query(By.css(".mainTitle"));
expect(h3Ele.nativeElement.textContent).toBe("Icons & Measurements")
})
it("Should render h3 tag with text 'Files from blower:' and span with information about current container", () => {
const h3Ele = fixture.debugElement.query(By.css(".headerh3"))
expect(h3Ele.nativeElement.innerHTML).toBe("Files from blower: <span _ngcontent-a-c391=\"\"></span>")
})
it("Should display current blower that user is at the moment in span", async () => {
const h3Ele = fixture.debugElement.query(By.css(".headerh3 span"))
// component.lowBlowId = "test_id"
// fixture.detectChanges()
debugger
})
})
然后我触发了这个 detectChanges() 我有这个错误:
我认为问题可能是我在 ts 文件中创建的一个函数中的参数。
async gettingLowBlowId() {
this.route.parent.params.subscribe((params) => {
this.lowBlowId = params.blower_id
})
}
这是我在测试中的设置:
let component: IconsInVisualizationComponent;
let fixture: ComponentFixture<IconsInVisualizationComponent>;
let administrationServiceMock: AdnimistrationSiteService;
let visualisationFileStorageManagerServiceMock: VisualisationFileStorageManagerService
beforeEach(async () => {
administrationServiceMock = jasmine.createSpyObj("AdnimistrationSiteService", ["getAllFilesWithIcons", "changeOrDeleteIcon", "getSensorsForMapping", "deleteSensorsAttachToIcon"])
visualisationFileStorageManagerServiceMock = jasmine.createSpyObj("VisualisationFileStorageManagerService", ["getFoldersList"])
await TestBed.configureTestingModule({
imports: [
HttpClientModule,
MatDialogModule,
BrowserAnimationsModule,
RouterTestingModule,
MatSnackBarModule,
MatTableModule,
],
providers: [
{provide: AdnimistrationSiteService, useValue: administrationServiceMock},
{provide: VisualisationFileStorageManagerService, useValue: visualisationFileStorageManagerServiceMock},
],
declarations: [ IconsInVisualizationComponent ],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(IconsInVisualizationComponent);
component = fixture.componentInstance;
});
你能帮我解决这个问题吗?
【问题讨论】:
标签: angular routes karma-jasmine angular-test