[英]How to assert not null?
我在 javascript 测试方面很新,我想知道如何在Mocha框架中断言不为空。
Mocha 支持您想要的任何断言库。 您可以在此处查看它如何处理断言: http : //mochajs.org/#assertions 。 不知道你想用哪一种。
考虑到您正在使用非常受欢迎的Chai ,这里有一些选择:
将“foo”视为您要测试的目标变量
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
,您甚至不需要 Chai。 就用它。 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;
PS:无关但在 Jest 上有一个toBeNull
函数。 你可以做expect(foo).not.toBeNull();
或expect(foo).not.toBe(null);
如果除了 Mocha 之外,您还使用 Chai:
assert.isNotNull(tea, 'great, time for tea!');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.