簡體   English   中英

如何為以下功能編寫單元測試用例

[英]How to write Unit test case for below function

下面的代碼正在運行,但代碼沒有按照預期的方式編寫。 因為函數正在返回一些東西,但我根據我的單元測試函數正在返回值

代碼:

checkSelectionFromDeletable() {
    return this.numbersFacade.selectedRows$.pipe(
      map(selectedRowCapsule =>
        selectedRowCapsule?.selectedRows.find(x => x.owner || x.mainNumberFlag)
      )
    );
 }

規格

it('should test checkSelectionFromDeletable', () => {
      const rowData  = of({
        phoneNumber: '12345678',
        extension: '',
        numberType: 'MAIN',
        state: 'US',
        location: '',
        locationId: '',
        mainNumberFlag: false,
        tollFreeFlag: false,
        owner: 'test'
      } as ICallNumberVM)
      component.checkSelectionFromDeletable();
      numbersFacade.selectedRows$.subscribe(res => {
        expect(res).toEqual(rowData);
      });
    });

您要測試的方法使用外部成員 ( this.numbersFacade.selectedRows$ ) 並返回一個 observable。 這意味着你應該:

  1. 在測試開始時初始化this.numbersFacade.selectedRows$Arrange部分)。
  2. 調用checkSelectionFromDeletable()並保存返回的 observable( Act部分)。
  3. 訂閱剛剛返回的 observable 並期望集合中的每個元素都定義了所有者或標志( Assert部分)。

例如:

it('should test checkSelectionFromDeletable', () => {
  const inputRows = [{ ... }, .........., { ... }];
  this.numbersFacade.selectedRows$ = of(inputRows); // Arrange
  const actualRows$ = component.checkSelectionFromDeletable(); // Act
  actualRows$.subscribe(actualRows => {
    const expectedRows = [{ ... }, .........., { ... }]; // Only those elements from 'inputRows' each of which having either owner or flag defined.
    // Assert: here you should expect 'expectedRows' to deeply equals 'actualRows'.
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM