Chai Framework API 不推荐否定
Chai Framework API does not recommend negation
所以,我正在阅读 Chai 框架的 API 指南,我发现每个 API 都有一个旁白 "However it is best to verify that the function returns the exact output , else it may lead to unexpected results"
在很多情况下,否定可以用来验证某个数组或对象是否包含或不包含值/键值对。例如:
expect(1).to.not.be.oneOf([2, 3, 4]);// Not Recommended
为什么不推荐这个,指南没有很好地解释这个?
原因对某些人来说似乎微不足道且显而易见,但如果有人能阐明这一点,我将不胜感激。谢谢!
这是我想出的一个例子:假设你有一个有点天真的函数,给定一种颜色,return 会产生那种颜色的水果:
function fruitByColor(color) {
return {
red : 'strawberry',
green : 'apple',
purple : 'grape',
}[color];
}
假设您正在使用此断言:
expect(fruitByColor('red')).to.not.be.oneOf([ 'apple', 'grape' ]);
这按预期工作,因为颜色 "red" returns "strawberry"。
但是,让我们使用另一个断言,这次是针对未定义的颜色:
expect(fruitByColor('yellow')).to.not.be.oneOf([ 'apple', 'grape' ]);
这仍然通过测试。从技术上讲,断言是正确的,但是您失去了传递现有颜色(return 是有效结果)和未定义颜色之间的区别。
所以,我正在阅读 Chai 框架的 API 指南,我发现每个 API 都有一个旁白 "However it is best to verify that the function returns the exact output , else it may lead to unexpected results"
在很多情况下,否定可以用来验证某个数组或对象是否包含或不包含值/键值对。例如:
expect(1).to.not.be.oneOf([2, 3, 4]);// Not Recommended
为什么不推荐这个,指南没有很好地解释这个?
原因对某些人来说似乎微不足道且显而易见,但如果有人能阐明这一点,我将不胜感激。谢谢!
这是我想出的一个例子:假设你有一个有点天真的函数,给定一种颜色,return 会产生那种颜色的水果:
function fruitByColor(color) {
return {
red : 'strawberry',
green : 'apple',
purple : 'grape',
}[color];
}
假设您正在使用此断言:
expect(fruitByColor('red')).to.not.be.oneOf([ 'apple', 'grape' ]);
这按预期工作,因为颜色 "red" returns "strawberry"。
但是,让我们使用另一个断言,这次是针对未定义的颜色:
expect(fruitByColor('yellow')).to.not.be.oneOf([ 'apple', 'grape' ]);
这仍然通过测试。从技术上讲,断言是正确的,但是您失去了传递现有颜色(return 是有效结果)和未定义颜色之间的区别。