使用ShouldJS比较来自Mongoose的数组
把一个数组(如'hello','there')存储在Mongoose文档中,
tags: { type: Array }
使用诸如:
Something.create({ tags: ['hello', 'there']}, cb);
然后使用ShouldJS来检查文档是否符合我提供的数组,我期望这一点:
doc.tags.should.eql(['hello', 'there']);
但事实并非如此。 如果我console.log doc标签我得到:
[hello, there]
请注意,报价已经消失。 doc.tags确实是一个数组(我可以检查instanceof数组),我也可以使用shouldjs
doc.tags.should.have.keys('hello'); doc.tags.should.have.keys('there');
任何人有一个想法,为什么我的arrays不匹配了?
你的数组不是一个真正的JSON Array
:它是一个MongooseArray
,其他方法。
为了让toObject()
数组一起工作,首先使用toObject()
:
doc.tags.toObject().should.eql(['hello', 'there']);