如何断言不为空?

我在JavaScripttesting非常新,我想知道如何在摩卡框架断言不为空。

摩卡支持你想要的任何断言库。 你可以看看它是如何处理断言的: http : //mochajs.org/#assertions 。 我不知道你想用哪一个。

考虑到你正在使用很受欢迎的Chai ,下面是一些选项:

考虑“foo”作为你想testing的目标variables

断言

var assert = chai.assert; assert(foo) // will pass for any truthy value (!= null,!= undefined,!= '',!= 0) // or assert(foo != null) // or assert.notEqual(foo, null); 

如果你想使用assert ,你甚至不需要柴。 只要使用它。 Node本身支持它: https : //nodejs.org/api/assert.html#assert_assert

应该

 var should = require('chai').should(); should.exist(foo); // will pass for not null and not undefined // or should.not.equal(foo, null); 

期望

 var expect = chai.expect; expect(foo).to.not.equal(null); // or expect(foo).to.not.be.null; 

这对我来说很有效(使用Mocha的 Expect库):

 expect(myObject).toExist('Too bad when it does not.');