Node中自动化测试断言包assert的方法说明

本文详细介绍了Node.js中的Assert断言模块,包括各种断言方法如assert.fail、assert.ok、assert.equal等的使用方法及应用场景。这些方法用于编写应用的单元测试,确保代码按预期运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Assent 断言模板包含如下方法:

assert.fail(actual, expected, message, operator)

assert.ok(value, [message])

assert.equal(actual, expected, [message])

assert.notEqual(actual, expected, [message])
assert.deepEqual(actual, expected, [message])
assert.notDeepEqual(actual, expected, [message])
assert.strictEqual(actual, expected, [message])
assert.notStrictEqual(actual, expected, [message])
assert.throws(block, [error], [message])
assert.doesNotThrow(block, [error], [message])
assert.ifError(value)


Assert 断言模块
This module is used for writing unit tests for your applications, you can access it with require('assert').
断言(Assert)模块用于为应用编写单元测试,可以通过require('assert')对该模块进行调用。


assert.fail(actual, expected, message, operator)
Tests if actual is equal to expected using the operator provided.
使用指定操作符测试actual(真实值)是否和expected(期望值)一致。


assert.ok(value, [message])
Tests if value is a true value, it is equivalent to assert.equal(true, value, message);
测试实际值是否为true,和assert.equal(true, value, message);作用一致


assert.equal(actual, expected, [message])
Tests shallow, coercive equality with the equal comparison operator ( == ).
使用等值比较操作符( == )测试真实值是否浅层地(shallow),强制性地(coercive)和预期值相等。


assert.notEqual(actual, expected, [message])
Tests shallow, coercive non-equality with the not equal comparison operator ( != ).
使用不等比较操作符( != )测试真实值是否浅层地(shallow),强制性地(coercive)和预期值不相等。


assert.deepEqual(actual, expected, [message])
Tests for deep equality.
测试真实值是否深层次地和预期值相等。


assert.notDeepEqual(actual, expected, [message])
Tests for any deep inequality.
测试真实值是否深层次地和预期值不相等。


assert.strictEqual(actual, expected, [message])
Tests strict equality, as determined by the strict equality operator ( === )
使用严格相等操作符 ( === )测试真实值是否严格地(strict)和预期值相等。


assert.notStrictEqual(actual, expected, [message])
Tests strict non-equality, as determined by the strict not equal operator ( !== )
使用严格不相等操作符 ( !== )测试真实值是否严格地(strict)和预期值不相等。


assert.throws(block, [error], [message])
Expects block to throw an error. error can be constructor, regexp or validation function.
预期block时抛出一个错误(error), error可以为构造函数,正则表达式或者其他验证器。


assert.doesNotThrow(block, [error], [message])
Expects block not to throw an error, see assert.throws for details.
预期block时不抛出错误,详细信息请见assert.throws。


assert.ifError(value)
Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, error in callbacks.
测试值是否不为false,当为true时抛出。常用于回调中第一个参数error的测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值