节点摩卡数组应该包含一个元素

我想做一个简单的断言

knownArray.should.include('known value') 

该数组是正确的,但我根本无法找出正确的断言用来检查数组是否有这个值(索引不重要)。 我也尝试了should.contain但是这两个都抛出一个错误,即Object #<Assertion> has no method 'contain' should.contain Object #<Assertion> has no method 'contain' (或'include'

我怎样才能检查一个数组包含使用should的元素?

Should.js有containsEql方法。 在你的情况下:

 knownArray.should.containEql('known value'); 

这些方法includeincludescontain你会发现在chai.js。

我喜欢柴油的东西很多:

https://github.com/RubenVerborgh/Chai-Things

让你做你喜欢的事情:

 [{ a: 'cat' }, { a: 'dog' }].should.include({ a: 'cat' }); ['cat', 'dog'].should.include('cat'); 

大部分来自摩卡文档 ,你可以做

 var assert = require('assert'); var should = require('should'); describe('Array', function(){ describe('#indexOf(thing)', function(){ it('should not be -1 when thing is present', function(){ [1,2,3].indexOf(3).should.not.equal(-1); }); }); }); 

或者如果你不介意不使用应该,你可以随时做

 assert.notEqual(-1, knownArray.indexOf(thing));