首页 > 解决方案 > 在异步函数中进行测试。测试通过什么时候应该失败

问题描述

大家好,这个测试应该失败,但它通过了,同时也抛出了断言错误。

 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)

如何让测试返回失败?

标签: javascriptnode.jschai

解决方案


catch子句中,您需要done使用错误调用,

    }).catch((e)=>{
        console.log(e);
        done(e);
    });

推荐阅读