角度测试未捕获错误:未捕获(承诺中):类型错误:无法读取空值的属性(正在读取'参数')错误、属性、角度、参数

2023-09-04 22:44:11 作者:左岸繁华*右岸殇か

我在测试组件时遇到问题。主要问题是当我运行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
      
    
    })
  })

但是,然后我触发了这一DetectChanges(),我得到了这个错误:

我认为问题可能是我在ts文件中创建的一个函数中的参数。

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
  async gettingLowBlowId() {
    this.route.parent.params.subscribe((params) => {
      this.lowBlowId = params.blower_id
    })
  }

SSIS读取Excel空值问题解决

这是我在测试中的设置:

数据-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 })
      }
    }
  }
],
 
精彩推荐
图片推荐