繁体   English   中英

如何断言不为空?

[英]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 中使用Expect库):

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

如果除了 Mocha 之外,您还使用 Chai:

assert.isNotNull(tea, 'great, time for tea!');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM