【问题标题】:Angular Testing Uncaught Error: Uncaught (in promise): TypeError: Cannot read properties of null (reading 'params')Angular 测试未捕获的错误:未捕获(承诺):TypeError:无法读取 null 的属性(读取“参数”)
【发布时间】: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


    【解决方案1】:

    在您的提供程序中,您可能需要像这样添加ActivatedRoute

    providers: [
      ...,
      {
        provide: ActivatedRoute,
        useValue: {
          parent: {
            params: of({ blower_id: 2 })
          }
        }
      }
    ],
    

    【讨论】:

    • 我添加了对此评论的回答作为对我问题的回答,因为我不想添加代码 sn-p
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2017-12-01
    • 2017-01-30
    相关资源
    最近更新 更多