【发布时间】:2021-03-20 07:33:33
【问题描述】:
大家好,我目前正在研究这个异步函数并为此编写单元测试,但它不起作用它说AssertionError: expected undefined to equal 'Everytime I think of coding, I am happy'这是我的函数代码:
async function funcFour() {// async automatically returns a promise
let result = new Promise((resolve) => {// returns a new promise named result
setTimeout(() => {
resolve(`Everytime I think of coding, I am happy`);//resolve happens after three seconds
}, 1500)
});
const response = await result;//closing resolve of new promise
console.log(response);//console.logging response of new promise
} funcFour();
这是我的单元测试:
describe("flirtFour()", () => {
it("Should return Everytime I think of coding, I am happy", async function () {
return flirtFour().then(result => {
expect(result).to.equal("Everytime I think of coding, I am happy")
})
})
})
这是我第一次编写单元测试,我正在尝试使用 async func 来完成它,所以我是新手。我真的很想看看这是怎么做到的,所以提前谢谢:)
【问题讨论】:
-
避免在 mocha 测试中使用箭头函数mochajs.org/#arrow-functions
标签: javascript node.js unit-testing mocha.js chai