Javascript 如何在摩卡进行测试?

Javascript 如何在摩卡进行测试?,javascript,arrays,node.js,mocha.js,chai,Javascript,Arrays,Node.js,Mocha.js,Chai,我不熟悉使用Mocha编写测试用例。我的nodejs中有以下功能。我想测试这个功能,但不知道该怎么做 const notes = []; if (this.note) { notes.push(this.note); } if (message) { notes.push(message); } 下面是我试图实现的 it("Approve the request", async () => { assert.notEqual(this.status, null); ass

我不熟悉使用Mocha编写测试用例。我的nodejs中有以下功能。我想测试这个功能,但不知道该怎么做

const notes = [];
if (this.note) { notes.push(this.note); }
if (message) { notes.push(message); }
下面是我试图实现的

it("Approve the request", async () => {
    assert.notEqual(this.status, null);
    assert.notEqual(this.status, "Pending");
    expect(typeof const === []).to.be.true;// I got stuck here
    assert.ok(true);
  })

我哪里出错了?

记住,测试用例将来自您的需求,而不是您的代码。因此,它应该涵盖需求中的所有情况,而不是代码。您的代码也需要涵盖所有需求

因此,从要求开始,我们可以说:

  • 该函数应将注释和消息添加到注释数组中

  • 如果note为空,则函数应忽略note

  • 如果消息为空,函数应忽略消息

  • 如果两个数组都为空,则函数应返回空数组


  • 因此,您可以开始基于这些需求编写测试

    您可以测试功能,或者如果您是单元测试的话,可以测试对象的方法。在这里写下简单的方法。