在异步函数中测试。应该失败时测试通过
Testing in async functions. Test passing when should be failing
大家好,这个测试应该失败了,但它通过了,同时也抛出了断言错误。
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)
如何让测试 return 不及格?
在catch
子句中,需要调用done
错误,
}).catch((e)=>{
console.log(e);
done(e);
});
致所有正在寻找此答案的人。 Shadab 是正确的,我需要调用 done(e)。我的最终解决方案看起来像
return driver.getAllRecipe().then((promise)=>{
assert.deepEqual(promise,'fake news')
}).catch((e)=>{
console.log(e);
done(e);
});
大家好,这个测试应该失败了,但它通过了,同时也抛出了断言错误。
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)
如何让测试 return 不及格?
在catch
子句中,需要调用done
错误,
}).catch((e)=>{
console.log(e);
done(e);
});
致所有正在寻找此答案的人。 Shadab 是正确的,我需要调用 done(e)。我的最终解决方案看起来像
return driver.getAllRecipe().then((promise)=>{
assert.deepEqual(promise,'fake news')
}).catch((e)=>{
console.log(e);
done(e);
});