javascript - 如何断言对象的子集存在于单元测试中?
问题描述
所以我有一个很大的嵌套字符串列表。大的嵌套列表最终会变得更大。但是我不希望当大的嵌套列表变大时单元测试会改变。
我正在返回自定义错误和爵士乐。但这与这个问题无关。所以我要把它抽象成一个水果列表。
我想要的是能够在不中断单元测试的情况下将项目添加到更大的列表中。换句话说,我想看看完整/干草堆/超集是否包括部分/针/子集。(所以没有deepEquals
)。我已经尝试了很多不同的方法来做到这一点,但我一直在做空。
摩卡规格文件
import "chai/register-assert";
import { assert } from "chai";
describe.only("Nested Lists and Objects", () => {
let allFoodList = {
smoothieStuff: ["apples", "bananas", "strawberries", "yogurt", "ice"],
saladStuff: ["apples", "carrots", "spinach", "strawberries"],
saladFruit: ["apples", "strawberries"],
};
let fruitList = {
smoothieStuff: ["apples", "bananas", "strawberries"],
saladStuff: ["apples", "strawberries"],
saladFruit: ["apples", "strawberries"],
};
// None of these work
it("deepNestedInclude", () => {
assert.deepNestedInclude(allFoodList, fruitList);
});
it("deepInclude", () => {
assert.deepInclude(allFoodList, fruitList);
});
it("include", () => {
assert.include(allFoodList, fruitList);
});
it.skip("notInclude", () => {
assert.notInclude(allFoodList, fruitList);
});
it("deepInclude", () => {
assert.deepInclude(allFoodList, fruitList);
});
it.skip("notDeepInclude", () => {
assert.notDeepInclude(allFoodList, fruitList);
});
it("nestedInclude", () => {
assert.nestedInclude(allFoodList, fruitList);
});
it("deepNestedInclude", () => {
assert.deepNestedInclude(allFoodList, fruitList);
});
it("ownInclude", () => {
assert.ownInclude(allFoodList, fruitList);
});
it.skip("notOwnInclude", () => {
assert.notOwnInclude(allFoodList, fruitList);
});
it("deepOwnInclude", () => {
assert.deepOwnInclude(allFoodList, fruitList);
});
it.skip("notDeepOwnInclude", () => {
assert.notDeepOwnInclude(allFoodList, fruitList);
});
it("sameMembers", () => {
assert.sameMembers(allFoodList, fruitList);
});
it("sameDeepMembers", () => {
assert.sameDeepMembers(allFoodList, fruitList);
});
it("includeMembers", () => {
assert.include(allFoodList, fruitList);
});
it("includeDeepMembers", () => {
assert.include(allFoodList, fruitList);
});
});
概括
0 passing (22ms)
4 pending
12 failing
我跳过了“不”测试,因为它们也会通过不包含所需项目的东西。我也没有放入任何“有序”的内容,因为我很确定这是一个额外的限制。
解决方案
你不能只使用一些辅助功能吗?:
const isSubset = (haystack, needles) =>
needles .every (needle => haystack .includes (needle))
const allAreSubsets = (haystackObj, needleObj) =>
Object .keys (needleObj) .every (key => isSubset (haystackObj[key] || [], needleObj[key]))
const allFoodList = {
smoothieStuff: ["apples", "bananas", "strawberries", "yogurt", "ice"],
saladStuff: ["apples", "carrots", "spinach", "strawberries"],
saladFruit: ["apples", "strawberries"],
}
const fruitList = {
smoothieStuff: ["apples", "bananas", "strawberries"],
saladStuff: ["apples", "strawberries"],
saladFruit: ["apples", "strawberries"],
}
const extraFruit = {
smoothieStuff: ["apples", "bananas", "strawberries"],
saladStuff: ["apples", "strawberries"],
saladFruit: ["apples", "strawberries", "kiwis"],
}
console .log (allAreSubsets (allFoodList, fruitList))
console .log (allAreSubsets (allFoodList, extraFruit))
isSubset
只是告诉 in 中的所有内容needles
是否也在haystack
. allAreSubsets
对 的每个属性执行相同的操作needlesObj
,将其与 的相同属性进行比较haystackObj
(如果不存在,则与空数组进行比较。)
包括类似的东西,你应该能够对布尔结果做出简单的断言。
推荐阅读
- mysql - MySQL-如何根据不同列但同一行中的值选择列?
- jquery - jquery 镜头变焦完美定位
- r - 提高计算大型气候数据栅格堆栈的速度
- python - 在 Maya 中创建文本生成工具的问题
- javascript - 通过点击一些改变两个“div”的颜色,通过Javascript编程
- javascript - 如何使用 Jasmine 在 AngularJS 1.5+ 组件中测试 $scope.$on
- c# - 文件不可访问 - Visual Studio 2017
- gruntjs - 干净维护的连续步骤——删除临时文件?
- c - C语言中关于scanf
- r - 将嵌套的tibble转换为R中的字符向量