为以下流星代码编写摩卡单元测试代码

时间:2019-06-07 12:56:54

标签: unit-testing testing meteor

我想知道如何使用流星功能的mocha编写测试代码

export const physicalToLogical = (physicalStatus, planningStartDate, planningEndDate) => {

  if(physicalStatus === STATUS_PHYSICAL_CREATING) {
    return STATUS_LOGICAL_CREATING;
  } else if (physicalStatus === STATUS_PHYSICAL_OPEN) {
    const now = new Date();
    if(planningStartDate.getTime() <= now && planningEndDate.getTime() > now) {
      return STATUS_LOGICAL_OPEN_FOR_PLAN;
    } else if(planningStartDate.getTime() > now) {
      return STATUS_LOGICAL_PROSPECT;
    }
    return STATUS_LOGICAL_REVIEW;
  } else if (physicalStatus === STATUS_PHYSICAL_CLOSED) {
    return STATUS_LOGICAL_CLOSED;
  } else if (physicalStatus === STATUS_PHYSICAL_ARCHIVED) {
    return STATUS_LOGICAL_ARCHIVED;
  }
  throw new Error("Not implemented yet");
};

1 个答案:

答案 0 :(得分:0)

首先,此功能与Meteor无关。

为该功能编写测试将涉及向该方法发送不同的状态并期望得到不同的结果。

下面是一个示例(使用chai作为断言库):

describe('physicalToLogical', () => {
  it('should return the given status', () => {
    expect(physicalToLogical(STATUS_PHYSICAL_CREATING, null, null)).
      toEqual(STATUS_LOGICAL_CREATING);
  });

  it('should...', () => {
    ...
  });

  ...
});

这是您必须为该代码编写的许多简单情况之一。
对于涉及日期的案例,还需要编写其他一些测试,但是格式大致相同。