我正在尝试编写测试聊天机器人的测试用例,并且我需要检查每个响应的 it
block 中的许多内容。所以现在的流程是我发送了很多消息,并且我试图在 forEach 循环内添加一个描述语句,但由于某种原因,这不起作用。 it
block 中的测试均未运行。
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
block 。我不知道如何重构它,以便拥有多个 it
block 来测试 API 的相同结果。
最佳答案
describe
需要 it
block ,您可以直接在其中编写代码。尝试使用钩子(Hook)来执行 API,然后测试
其响应。
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()
})
});
关于node.js - 如何在循环中使用描述?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55688192/