javascript - 在异步函数中进行测试。测试通过什么时候应该失败
问题描述
大家好,这个测试应该失败,但它通过了,同时也抛出了断言错误。
describe('Testing getAllrecipes',()=>{
it('1. Test get All Recipes',(done)=>{
var uri = "mongodb://localhost:27017";
var dbname = "testRecipes";
var collectionName = "testCollectionClean";
let driver = new MongoDriver(uri,dbname,collectionName);
driver.dropCollection(); //Clean collection for testing... NEVER CALL ON PRODUCTION COLLECTION
driver.addMockData();
driver.getAllRecipe().then((promise)=>{
assert.deepEqual(promise,'fake news')
done();
}).catch((e)=>{
console.log(e);
done();
});
});
})
安慰:
AssertionError: expected [] to deeply equal 'fake news'
{
showDiff: true,
actual: [],
expected: 'fake news'
}
✓ 1. Test get All Recipes
8 passing (60ms)
如何让测试返回失败?
解决方案
在catch
子句中,您需要done
使用错误调用,
}).catch((e)=>{
console.log(e);
done(e);
});
推荐阅读
- python - 从命令行传递参数作为列表并解析它
- java - 具有 Serenity 显式等待的 Selenium 在返回真实条件后继续执行
- javascript - 带有重命名的 ASP.NET Core AzureBlob 下载文件
- react-native - 使用 React Native 将图像上传到 Firebase
- jenkins - 源代码管理设置未保存在詹金斯作业配置中
- php - Laravel - 数据没有通过形成
- java - Kotlin Flows Java 互操作回调
- regex - 带有正则表达式的 awk 中的自定义字段分隔符:前导空格和逗号的问题
- c - 将结构指针传递给函数
- c - 使用 POSIX.1-2001 创建临时目录