如何断言不为空? [英] How to assert not null?

查看:265
本文介绍了如何断言不为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript测试中是一个新手,我想知道如何在 Mocha 框架中断言不为null.

I'm very new in javascript testing, I would like to know how to assert not null in Mocha framework.

推荐答案

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

Mocha supports any assertion library you want. You can take a look at how it deals with assertions here: http://mochajs.org/#assertions. I don't know which one you want to use.

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

Considering you are using Chai, which is pretty popular, here are some options:

将"foo"视为要测试的目标变量

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,甚至不需要柴.只需使用它. Node本身支持它: https://nodejs.org/api/assert.html#assert_assert

In case you want to use assert, you don't even need Chai. Just use it. Node supports it natively: 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);

PS: Unrelated but on Jest there is a toBeNull function. You can do expect(foo).not.toBeNull(); or expect(foo).not.toBe(null);

这篇关于如何断言不为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆