我有一个返回comparisonFunction功能
getComparisonFunction(propertyOfComparison) {
const func = function(a, b){
if ( a[propertyOfComparison] < b[propertyOfComparison] ) {
return -1;
}
if ( a[propertyOfComparison] > b[propertyOfComparison] ) {
return 1;
}
return 0;
};
return func;
}
此方法将里面的javascript“分类”方法一起使用。 例如:
arrayOfObjects.sort(getComparisonFunction('name'));
这种方法将“arrayOfObjects”的“名称”属性进行排序。 方法工作正常,问题是:如何我可以比较函数调用不同的参数
it('should get correct comparison function', function () {
const func = component.getComparisonFunction('testProperty');
const expectedFunc = function(a, b){
if ( a['testProperty'] < b['testProperty'] ) {
return -1;
}
if ( a['testProperty'] > b['testProperty'] ) {
return 1;
}
return 0;
};
expect(func.toString()).toEqual(expectedFunc.toString());
});
这是我现在有,但它没有工作。 我运行代码后收到的错误:
Expected 'function (a, b) {
if (a[propertyOfComparison] < b[propertyOfComparison]) {
return -1;
}
if (a[propertyOfComparison] > b[propertyOfComparison]) {
return 1;
}
return 0;
}' to equal 'function (a, b) {
if (a['testProperty'] < b['testProperty']) {
return -1;
}
if (a['testProperty'] > b['testProperty']) {
return 1;
}
return 0;
}'.
检查函数的代码作为测试非常脆,容易折断给你一个假阴性:
let someFn = function(a, b) { return a + b; } let expected = `function(a, b) { return a + b; }` console.log("Test original implementation:", test(someFn.toString(), expected)); //later the code style is changed to remove extra whitespace and make it one line someFn = function(a, b) { return a+b; } console.log("Test updated implementation:", test(someFn.toString(), expected)); //simple testing function test(expected, actual) { return expected == actual }
该代码打破了测试只是让非功能性的改变。
更糟糕的是,如果有对码功能的变化,测试不能保证新实现的行为类似于旧的,因为它只着眼于代码的结构:
//simplified case of what the actual code could be doing function someCodeBaseFunction() { let someInput = [8, 12, 42]; return someFn(...someInput) } let someFn = function(a, b) { return a+b; } let expected = `function(a, b) { return a+b; }` console.log("Test original implementation:", test(someFn.toString(), expected)); console.log("Codebase usage:", someCodeBaseFunction()); //20, as the third number is ignored //new implementation someFn = function(...args) { return args.reduce((a, b) => a + b); } //update the test, so it passes expected = `function(...args) { return args.reduce((a, b) => a + b); }` console.log("Test updated implementation:", test(someFn.toString(), expected)); //some existing line of code console.log("Codebase usage:", someCodeBaseFunction()); //62, as the third number is now used //simple testing function test(expected, actual) { return expected == actual };
相反,你想要做什么测试代码的行为 ,并设置您的期望存在。 这样一来,如果实现更改,可以确保执行仍符合同一组的期望。
在这种情况下,你需要创建一个样本输入端,最初是无序的,试图订购,然后想到的是,为了工作,你的预期。 在伪代码,看起来有点像这样:
//arrange
input = [
{testProperty: "c", id: 1},
{testProperty: "a", id: 2},
{testProperty: "d", id: 3},
{testProperty: "b", id: 4}
];
expected = [
{testProperty: "a", id: 2},
{testProperty: "b", id: 4},
{testProperty: "c", id: 1},
{testProperty: "d", id: 3}
];
//act
input.sort(component.getComparisonFunction('testProperty'))
//assert
expect(input).toEqual(expected);
您也可以在更细化的级别添加更多的测试,以更加绑定的期望,如果你想。 例如,如果你想确保比较是区分大小写
//arrange
a = { testProperty: "a" };
b = { testProperty: "B" };
//act
result = component.getComparisonFunction('testProperty')(a, b)
//assert
expect(result).toBeGreaterThanOrEqual(1)
或不区分大小写:
//arrange
a = { testProperty: "a" };
b = { testProperty: "B" };
//act
result = component.getComparisonFunction('testProperty')(a, b)
//assert
expect(result).toBeLessThanOrEqual(-1)
这更清楚地定义你的期望,并确保未来的变化将涵盖正是你需要的。
如果你想通过任何PARAM提供实现排序,你可以尝试以下操作:
const array=[
{name:'C',Key:'14',val:3},
{name:'B',Key:'12',val:2},
{name:'A',Key:'11',val:1},
{name:'D',Key:'16',val:4},
{name:'E',Key:'18',val:5}
];
console.log(array);
function comparer(prop){
return function(a,b){
return a[prop]-b[prop];
}
};
array.sort(comparer('Key'));
console.log(array);
array.sort(comparer('val'));
console.log(array);
此外,为了测试它只需测试用例像上面,并检查其是否按照您的实现分拣。
文章来源: How to test two returned functions with different argument value in javascript?