角度测试未捕获错误:未捕获(承诺中):类型错误:无法读取空值的属性(正在读取';参数';) [英] Angular Testing Uncaught Error: Uncaught (in promise): TypeError: Cannot read properties of null (reading 'params')
本文介绍了角度测试未捕获错误:未捕获(承诺中):类型错误:无法读取空值的属性(正在读取';参数';)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在测试组件时遇到问题。主要问题是当我运行Fixture.DetectChanges()时。 当我没有触发它时,所有的测试都通过了,没有任何问题。 以下是测试:
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假"> 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
})
})
我认为问题可能是我在ts文件中创建的一个函数中的参数。
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假"> async gettingLowBlowId() {
this.route.parent.params.subscribe((params) => {
this.lowBlowId = params.blower_id
})
}
这是我在测试中的设置:
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假"> 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;
});
您能帮我解决这个问题吗?
推荐答案
在您的提供程序中,您可能需要像这样添加ActivatedRoute
:
providers: [
...,
{
provide: ActivatedRoute,
useValue: {
parent: {
params: of({ blower_id: 2 })
}
}
}
],
这篇关于角度测试未捕获错误:未捕获(承诺中):类型错误:无法读取空值的属性(正在读取';参数';)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!