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

javascript - chai eql reports equal arrays as different - Stack Overflow

programmeradmin0浏览0评论

I am testing a function in a node.js program, which should save some data to a mongo database. I am using mocha, chai and should.

In the schema, I have defined the data to be saved as

data: [{type: Number, min: 0}]

The test saves the array [1,2,3,4,5,6] to the database, then finds it again, and pares what it found with the array that was saved. The test is

result.data.should.eql([1,2,3,4,5,6]);

The test result is

Uncaught AssertionError: expected [1,2,3,4,5,6] 
to deeply equal [ 1, 2, 3, 4, 5, 6 ]

According to this chai.js example, paring arrays this way should work just fine.

When I test the result with result.data.should.deep.include.members([1,2,3,4,5,6]);, the test passes just as expected.

Can anyone explain to me why this is not working?

I am testing a function in a node.js program, which should save some data to a mongo database. I am using mocha, chai and should.

In the schema, I have defined the data to be saved as

data: [{type: Number, min: 0}]

The test saves the array [1,2,3,4,5,6] to the database, then finds it again, and pares what it found with the array that was saved. The test is

result.data.should.eql([1,2,3,4,5,6]);

The test result is

Uncaught AssertionError: expected [1,2,3,4,5,6] 
to deeply equal [ 1, 2, 3, 4, 5, 6 ]

According to this chai.js example, paring arrays this way should work just fine.

When I test the result with result.data.should.deep.include.members([1,2,3,4,5,6]);, the test passes just as expected.

Can anyone explain to me why this is not working?

Share Improve this question asked Nov 20, 2014 at 8:30 larlonlarlon 5871 gold badge6 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Mongoose decorates Arrays with a lot of its own methods. You won't see these when you do console.log(result.data), but deep-eql (the library that Chai uses to do deep equality) will consider them when doing parison. This is why the test fails when you use .eql(): you are paring a special Mongoose Array to a plain vanilla Array.

Rather, if you did

result.data.toJSON().should.eql([1,2,3,4,5,6]);

your test would pass as you expect.

The .members() assertion goes through a different code path, which is why it happens to work.

As an aside, the example that worked for you doesn't do exactly what you want, it is a superset test. What you actually want is:

result.data.should.have.members([1,2,3,4,5,6]);
发布评论

评论列表(0)

  1. 暂无评论