我应该通过说我以前从未做过单元测试来开头所有内容,因此,如果您对我而言是明确的,而不是假定您具有高级实践知识。比你好。
我正在尝试创建一个单元测试,以检查以确保每个键"x"
的值为"y"
。我的JavaScript将"a"
,"b"
或"c"
传递到module.exports.handler = async (letter) =>
中。
这会根据参数"a"
,"b"
或"c"
过滤 JSON ,并返回带有键/值对的对象数组。
如果传入"a"
,则键为"x"
的对象数组都具有值"y"
;
如果将"b"
作为参数传递,则键为"x"
的对象数组的值为"z"
;
最后,如果传入参数"c"
,则键为"x"
的对象数组的值为"w"
。
describe('filtering spec', () => {
it('should return an array of objects each of which with y as the value', async () => {
// Makes sure the returned array of objects all have y as the corresponding value for key x
});
it('should return an array of objects each of which with z as the value', async () => {
// Makes sure the returned array of objects all have z as the corresponding value for the key x
});
it('should return an array of objects each of which with y as the value', async () => {
// Makes sure the returned array of objects all have w as the corresponding value for the key x
});
我猜我最终将以某种方式使用matcher
(请参阅https://sinonjs.org/releases/latest/matchers/)
答案 0 :(得分:1)
我建议您使用Array.Filter方法来查看是否有任何对象具有不需要的值,如果有任何,则您的测试应该失败。
peso = str(peso)
如评论中所述,Array.some实际上至少快了 18%!
let array = [{x: "y"}, {x: "y"}, {x: "y"}, {x: "b"}]
isCorrect = (array, req) => {
return (array.filter(v => v.x !== req))
}
console.log(isCorrect(array, "y").length ? "failed" : "passed")
您还可以使用Array.includes ...