[英]Angular Testing Uncaught Error: Uncaught (in promise): TypeError: Cannot read properties of null (reading 'params')
I have problem with testing my component.我在测试我的组件时遇到问题。 The main problem is when I run fixture.detectChanges().
主要问题是当我运行 fixture.detectChanges() 时。 When I do not fire it, all tests pass without any problems.
当我不开火时,所有测试都毫无问题地通过。 This is the tests:
这是测试:
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 }) })
But then i fire this detectChanges() i have this error:但是后来我触发了这个 detectChanges() 我有这个错误:
I think the issue may be params in one function i created in ts file.我认为问题可能是我在 ts 文件中创建的一个函数中的参数。
async gettingLowBlowId() { this.route.parent.params.subscribe((params) => { this.lowBlowId = params.blower_id }) }
This is my set up in tests:这是我在测试中的设置:
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; });
Can you help me with this issue?你能帮我解决这个问题吗?
In your providers you probably need to add the ActivatedRoute
like this :在您的提供者中,您可能需要像这样添加
ActivatedRoute
:
providers: [
...,
{
provide: ActivatedRoute,
useValue: {
parent: {
params: of({ blower_id: 2 })
}
}
}
],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.