最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MochaChai testing expected vs actual arrays of objects - Stack Overflow

programmeradmin4浏览0评论

What's the best way to assert that the expected results matches the actual results when both are arrays of objects? My immediate thought was to use Array prototype filter and check that the intersection is the same size as expected, i.e.:

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);
  });
});

Does this approach make sense? Is there a better way to assert the query results match expectations?

What's the best way to assert that the expected results matches the actual results when both are arrays of objects? My immediate thought was to use Array prototype filter and check that the intersection is the same size as expected, i.e.:

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);
  });
});

Does this approach make sense? Is there a better way to assert the query results match expectations?

Share Improve this question edited Jan 1, 2014 at 2:59 Pauli Price asked Jan 1, 2014 at 0:11 Pauli PricePauli Price 4,2373 gold badges35 silver badges62 bronze badges 1
  • btw - I'm testing the query language implementation, so the query is the sut – Pauli Price Commented Jan 1, 2014 at 0:14
Add a comment  | 

2 Answers 2

Reset to default 14

As you noted, for sets, you can use:

expect(actual).to.have.members(expected);

This will fail if there are any members in expected that are not in actual, and vice versa.

You can also do:

expect(actual).to.contain.members(expected);

Which will fail if there are members in expected that are not in actual, but will not fail if there are members in actual that are not in expected. (In other words, actual must be a superset of expected).

If you're comparing objects, you probably want to deep compare:

expect(actual).to.deep.have.same.members(expected);

e.g.:

expect([{a:'a'}, {a:'b'}]).to.deep.have.same.members([{a:'a'}, {a:'b'}]);

(edited: to.have.same.members() is actually the same as to.have.members().)

Chai has a members matcher that does what I need:

expect(actual).to.have.members(expected);
发布评论

评论列表(0)

  1. 暂无评论