我是javascript测试方面的新手,我想知道如何在Mocha框架中断言NOTNULL.

推荐答案

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,你甚至不需要柴.就用它吧. 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);

Node.js相关问答推荐

使用xml-crypto时出现NodeJS XPath解析错误

Sequelize-测试使用虚拟场更新模型

如何从 Mongo Atlas 触发器向 GCP PubSub 发出经过身份验证的请求

$not 的聚合版本是什么?

npm 在 Windows 终端中不工作

我正在try 在公共目录中使用 Express.js 项目部署 Angular 静态构建

为什么 nginx 不将我的 react index.html 作为后备服务

在 .htaccess 中从非 www 切换到 www 后如何解决无法访问该站点?

firebase/messaging 不提供名为 getToken 的导出

Mocha调用所有it回调模拟(测试中间件)

在系统启动时启动本地 node 服务器

node.js 中 pdfkit-tables 中的垂直线

Node JS:自动 Select `http.get`与`https.get`

如何在 Node.js 的 console.log() 中创建换行符

AWS Kinesis 中的分区键是什么?

如何在 NodeJs 中下载和解压缩内存中的 zip 文件?

将变量传递给nodemailer中的html模板

在 Node.js 中使用公钥加密数据

Meteor - collection.find() 总是返回所有字段

路由后的 Node Express 4 中间件