【发布时间】:2020-08-29 15:35:57
【问题描述】:
我有 MongoDB Query 函数,其中验证了查询参数 这是功能 注意:用户是猫鼬模型
function fetchData(uName)
{
try{
if(isParamValid(uName))
{
return user.find({"uName":uName}).exec()
}
else {
throw "Invalid params"
}
}
catch(e)
{
throw e
}
}
为了使用无效的用户名值对此进行测试,我已经为基于承诺的函数使用 mocha、chai 和 chai-as-promised 编写了测试代码
describe('Test function with invalid values', async ()=>{
it('should catch exception', async () => {
await expect(fetchData(inValidUserName)).to.throw()
})
it('should catch exception', async () => {
await expect(fetchData(inValidUserName)).to.throw(Error)
})
it('should catch exception', async () => {
await expect(fetchData(inValidUserName)).to.be.rejectedWith(Error)
})
it('should catch exception', async () => {
await expect(fetchData(inValidUserName)).to.be.rejected
})
})
他们都没有通过测试,我如何编写测试用例来处理无效用户名值的异常
【问题讨论】:
标签: javascript node.js mocha.js chai chai-as-promised