gpt4 book ai didi

javascript - 如何断言不为空?

转载 作者:IT老高 更新时间:2023-10-28 21:57:24 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com