如何为行为主题编写单元测试

how to write unit test for behavior subject

 @Input() public openDrawer: BehaviorSubject<{
    open: boolean;
    assetConditionDetails: AssetConditionIdDetails[];
    selectedAssets: SelectedAssets[];
  }>;

 public ngOnInit(): void {
    this.openDrawer.subscribe((result) => {
      if (result) {
        this.showLoader = result.open;
        this.isDrawerActive = result.open;
        this.selectedAssets = result.selectedAssets;
        this.assetConditionDetails = result.assetConditionDetails;
      }
    });
  }

有人可以告诉我如何为此编写单元测试用例吗? 这是我写的,但它说“失败:无法读取未定义的 属性 'subscribe'”

it('should get users', async(() => {
    component.openDrawer.subscribe(result => {
      fixture.detectChanges()
      expect(result.open).toBe(component.showLoader)
    })
  }))

试试这个:

it('should get users', () => {
 // mock openDrawer
  component.openDrawer = new BehaviorSubject<any>({ open: true, assetConditionDetails: [], selectedAssets: [] });
  // explicitly call ngOnInit
  component.ngOnInit();

  expect(component.showLoader).toBe(true);
  expect(component.isDrawerActive).toBe(true);
  expect(component.selectedAssets).toEqual([]);
  expect(component.assetConditionDetails).toEqual([]);
});