当两者都是对象数组时,断言预期结果与实际结果匹配的最佳方法是什么?我的直接想法是使用 Array 原型过滤器并检查交集是否与预期的大小相同,即:
describe('select',function(){
it("should return selected columns", function(done) {
var query = "select lunchTime, name";
var actual = ... results of the query, an array of anonymous objects ...
// expected results
var expected = [{"lunchTime": "12:00:00", "name": "John"},
{"lunchTime": "12:00:00", "name": "Dave"},
{"lunchTime": "13:00:00", "name": "Sally"},
{"lunchTime": "12:00:00", "name": "Ben"},
{"lunchTime": "12:00:00", "name": "Dana"},
{"lunchTime": "13:00:00", "name": "Mike"}];
var intersection = actual.filter(function(n) {
return expected.indexOf(n) != -1
});
expect(intersection).to.have.length(expected.length);
expect(actual).to.have.length(expected.length);
});
});
这种方法有意义吗?有没有更好的方法来断言查询结果符合预期?