节点mocha数组应包含一个元素

Exp*_*lls 9 arrays mocha.js node.js should.js

我想做一个类似的简单断言

knownArray.should.include('known value')
Run Code Online (Sandbox Code Playgroud)

数组是正确的,但我根本无法弄清楚用于检查数组是否具有此值的正确断言(索引无关紧要).我也试过,should.contain但这两个都抛出一个错误Object #<Assertion> has no method 'contain'(或'include')

如何检查数组是否包含使用的元素should

Rod*_*ros 15

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

knownArray.should.containEql('known value');
Run Code Online (Sandbox Code Playgroud)

的方法include,includes并且contain你会发现在chai.js.


And*_*rew 5

主要来自 mocha 文档,您可以这样做

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);
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

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

assert.notEqual(-1, knownArray.indexOf(thing));
Run Code Online (Sandbox Code Playgroud)