javascript - 使用 Chai+Mocha 测试失败的自定义特定错误消息?
问题描述
我正在为一个函数编写一个单元测试,并让它检查几十条测试数据。如果该函数不适用于其中一个测试用例,我希望它显示该函数失败的特定数据。
在我的例子中,我在后端有大约一百个函数我正在为其编写测试,并且我正在编写一些 Chai.jsassert()
案例来检查许多测试数据。我不想让它跨数十条数据打印出相同功能的单个测试,因为我希望在整个后端运行测试时保持日志可读。
这是我目前编写测试的方式:
context('relative urls should be removed', function () {
it('data should initially contain relative urls', async function () {
assert(testHTML.every(containsRelativeURLs));
});
// (run testHTML through my function here)
it('data should end with no relative urls', async function () {
assert(!testHTML.every(containsRelativeURLs));
})
});
如果我误解了编写单元测试的正确方法,请告诉我。如果有更清洁或更标准的方法来做到这一点,我很想知道。
解决方案
assert
可以带两个参数——第一个是你的断言,第二个是消息。您可以使用消息参数来记录信息。
describe('it passes all the tests /', ()=>{
testData.forEach(testAsync =>
it('should pass this test', async (done) =>
const result = await testAsync
assert(result === expectedResult, "bad ${result} returned by testAsync")
done()
)
})
推荐阅读
- javascript - 如何将重写的原型方法“重置”回其原始行为
- regex - 在 bash 中使用正则表达式获取字符串的子字符串
- jenkins - Jenkins jnlp slave 不在本地 Kubernetes 集群上工作
- javascript - Can a jquery script be invoked AFTER a PHP file has injected content into a HTML document?
- express - 如何修复来自 busboy 的“由于多部分数据意外结束而导致部分提前终止”错误
- python - 如何将 tqdm 用于 JSON 文件加载进度条?
- python - Python中调用的函数中的函数是什么?
- android - 如何从 Xamarin 中的活动中获取视图
- hadoop - 独立 Metastore 3.0 是否需要 Hadoop?
- android - 在安卓平板电脑上使用 python