javascript - 如何断言不为空?

标签 javascript node.js mocha.js

我是 javascript 测试的新手,我想知道如何在 Mocha 框架中断言 not null。

最佳答案

Mocha 支持任何你想要的断言库。您可以在此处查看它如何处理断言:http://mochajs.org/#assertions .不知道你想用哪一个。

考虑到您正在使用 Chai ,这很受欢迎,这里有一些选项:

Consider "foo" to be the target variable you want to test

断言

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);

关于javascript - 如何断言不为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29464849/

相关文章:

javascript - 使用 mocha 对 Node 模块进行单元测试,模块变量行为异常

javascript - 对 Angular JS Promise 的通知函数进行单元测试时,永远不会调用通知

javascript - jQuery if..else 基于hash id 提取的条件语句

javascript Reg Backwards 不起作用

node.js 什么是 requirejs 模块

javascript - 遍历嵌套在 JSON 中的结果数组

javascript - __doPostBack 函数是什么意思,什么时候使用?

javascript - Google Analytics 中的事件跟踪功能存在问题

javascript - Nodejs/Expressjs JavaScript : get where request came from

node.js - 为什么使用 supertest 而不是 expressJS 的单元测试?