如何在循环中使用 describe?

How can I use describe in a loop?

我正在尝试编写测试聊天机器人的测试用例,我需要在每个响应的 it 块中检查许多内容。所以现在的流程是我发送了很多消息,我试图在 forEach 循环中有一个 describe 语句,但由于某种原因,这是行不通的。 it 块中的 None 个测试是 运行。

const body = ['hi', 'transfer 20 sms', 'no', 'no', 'first one', 'first one']

describe('API', () => {
      describe('Basic flow', () => {
        body.forEach((v, i) => {
          describe(`Should get response for message #${i + 1}`, () => {
            return agent.post('/watson/send').send({
              'content': {
                'userInput': v,
                'userDial': '123456'
              }
            }).then(response => {
              it('Body should exist', done => {
                // this part doesnt work
                const { body } = response
                const { text } = response.body.reply
                expect(_.isEmpty(body)).to.equal(false)
                done()
              })
            })
          })
        })
      })
    })

我的理解是这不起作用,因为 mocha 没有在 promise 中找到 it 块。我不知道如何重组它以便有多个 it 块来测试 API.

的相同结果

describe 期望 it 块,您直接在其中编写代码。尝试使用钩子来执行 API 然后 test 它的响应。

describe('Should get response for message', function () {

    let _response;

    // before hook
    before(function () {

        return agent.post('/watson/send').send({
            'content': {
                'userInput': v,
                'userDial': '123456'
            }
        }).then(response => {
            _response = response;
            done();
        })
    });

    // test cases
    it('Body should exist', done => {
        // this part doesnt work
        const { body } = _response
        const { text } = _response.body.reply
        expect(_.isEmpty(body)).to.equal(false)
        done()
    })
});