简体   繁体   English

角度测试未捕获错误:未捕获(承诺):类型错误:无法读取 null 的属性(读取“参数”)

[英]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.

相关问题 错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性(读取“删除”) - ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'remove') angular 2:Uncaught(在promise中):TypeError:无法读取null的属性'setParent' - angular 2: Uncaught (in promise): TypeError: Cannot read property 'setParent' of null 错误错误:未捕获(承诺):TypeError:无法读取null的属性“ push” - ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'push' of null ERROR 错误:未捕获(承诺中):TypeError:无法读取 null 的属性“isAdmin” - ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'isAdmin' of null 错误错误:未捕获(承诺):TypeError:无法读取null的属性“ child” - ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'child' of null 当我将 javascript 文件导入 angular 时,我发现 Uncaught TypeError: Cannot read properties of null (reading &#39;querySelector&#39;) - when i import javascript files to angular i found Uncaught TypeError: Cannot read properties of null (reading 'querySelector') 错误:未捕获(承诺中):TypeError:无法读取 null 的属性“状态” - Error: Uncaught (in promise): TypeError: Cannot read property 'status' of null 如何调试Angular错误 - 错误:未捕获(在promise中):TypeError:无法读取null的属性“data” - How to debug Angular error - Error: Uncaught (in promise): TypeError: Cannot read property 'data' of null 错误错误:未捕获(承诺):类型错误:无法读取 null 的属性“订阅”(离子角) - ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'subscribe' of null (Ionic Angular) 错误:未捕获(承诺中):TypeError:无法读取属性 - Error: Uncaught (in promise): TypeError: Cannot read property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM