【问题标题】:unit test for a function that returns nothing对不返回任何内容的函数进行单元测试
【发布时间】:2022-01-25 12:18:57
【问题描述】:

我想为 loadingTime 函数编写一个单元测试。该函数将timeTable(类型map)转换为一个由0和1组成的结果数组。索引3-5(03:00-05:00),12-14(12:00-14:00)为1,其余为1为0。然后将结果数组分配给rowTable。如何为此编写单元测试?

timeTable: Map<number, Array<from: string to: string>>

0:[{from: "03:00", to: 05:00"}, {from: "12:00", to:"14:00"}]
1:[{from: "00:00", to: "23:00"}]
2:[{from: "00:00", to: "23:00"}]
3:[{from: "00:00", to: "23:00"}]
4:[{from: "00:00", to: "23:00"}]
5:[{from: "00:00", to: "23:00"}]
6:[{from: "00:00", to: "23:00"}]

行表:

 rowTable: Row[] = [
    { name: this.allDays.monday, items: new Array(24).fill(1), active: true },
    { name: this.allDays.tuesday, items: new Array(24).fill(1), active: true },
    { name: this.allDays.wednesday, items: new Array(24).fill(1), active: true },
    { name: this.allDays.thursday, items: new Array(24).fill(1), active: true },
    { name: this.allDays.friday, items: new Array(24).fill(1), active: true },
    { name: this.allDays.saturday, items: new Array(24).fill(1), active: true },
    { name: this.allDays.sunday, items: new Array(24).fill(1), active: true }
  ];

加载时间

loadingTime(): void {
   if (this.timeTable) {
     let result = [...this.timeTable].reduce((r, [key, value], i) => {
       r[i] = Array(24).fill(0);
       value.forEach(o => {
         let start = getHours(o.from);
         const end = getHours(o.to);
         while (start <= end) {
           r[i][start] = 1;
           start++;
         }
       })
       return r;
     }, []);
 
     this.rowTable.forEach((el, i) => {
       el.items = result[i];
       el.active = false;
     })
   }
 }

我的尝试,但我不知道我是否朝着正确的方向前进。请帮忙

it("should loading Time"), () => {

  const timetableMock = new Map<number, Array<from: string to: string>>()
      timetableMock.set(0, [{ from: '00:00', to: '23:00' }]);

  component.loadingTime = () => {};

  const onSaveSpy = spyOn(component, "loadingTime")

  component.loadingTime();

  expect(onSaveSpy).toEqual([........I don't know what to enter here]);

}

【问题讨论】:

  • 您无法检查返回的值。但是你可以检查getHours是否被调用。
  • 测试行为。用户应该看到什么?不要模拟你应该测试的部分 - 你的测试目前绝对没有意义,因为你用无操作实现替换方法,然后监视该实现,然后调用间谍并断言。 ..间谍被召唤了?真正的实现是如何涉及的?

标签: javascript angular typescript jestjs


【解决方案1】:

这样的东西会起作用吗?

it('loadingTime should not update rowTable if timeTable is undefined', () => {
  const rowTableForEachSpy = jest.spyOn(component.rowTable, 'forEach');
  component.timeTable = void 0;
  component.loadingTime();
  expect(rowTableForEachSpy).not.toHaveBeenCalled();
  // in addition you can most probably assert `component.rowTable` value here
})

it('loadingTime should update rowTable if timeTable is defined', () => {
  const rowTableForEachSpy = jest.spyOn(component.rowTable, 'forEach');
  component.timeTable = new Map(...);
  component.loadingTime();
  expect(rowTableForEachSpy).toHaveBeenCalled();
  // in addition you can most probably assert `component.rowTable` value here
})

【讨论】:

  • 我有错误:TypeError: Cannot set properties of undefined (setting 'timeTable')
  • 在哪种情况下?您尚未共享组件或规范的完整代码。此错误意味着 timeTable 未定义,您正在尝试设置其属性。为什么变量未定义?你的组件是强类型的吗?如果你在你的例子中声明timeTable: Map&lt;number, Array&lt;from: string to: string&gt;&gt;(即没有初始化时间表的值),它应该是timeTable?: Map&lt;number, Array&lt;from: string to: string&gt;&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多