美文网首页
【node】assert 断言

【node】assert 断言

作者: 大Q本Q | 来源:发表于2019-07-10 10:10 被阅读0次

引入assert

const assert = require('assert')

创建错误消息 AssertionError

调用:

interface O{
    message<string>
    actual<any>,
    expected<any>,
    operator<string>,
    stackStartFn
}
new asset.AssertionError(options:O)

手动创建

// 创建
let message = new assert.AssertionError({
    actual:1,
    expected:2,
    operator:'strictEqual'
})
console.log(message);

/* 输出:
{ AssertionError [ERR_ASSERTION]: 1 strictEqual 2
    at new AssertionError (internal/errors.js:83:11)
    at Object.<anonymous> (/STORE/node/hello-world.js:4:15)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3
  generatedMessage: true,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: 1,
  expected: 2,
  operator: 'strictEqual' }
*/

自动创建

try {
    assert.strictEqual(1,2);  // 程序报错时,自动创建错误
}catch(err){
    console.log(err);
}

/* 输出:
{ AssertionError [ERR_ASSERTION]: 'a' === 'b'
    at Object.<anonymous> (/STORE/node/hello-world.js:5:9)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3
  generatedMessage: true,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: 'a',
  expected: 'b',
  operator: '===' }
*/

相关文章

  • Node.js Assertion Testing

    Assert Stability: 2 - Stable 稳定 Assert - Node.js 的断言库 在 N...

  • 【node】assert 断言

    引入assert 创建错误消息 AssertionError 调用: 手动创建 自动创建

  • (1)Node.js之assert

    assert用来做什么? assert作为Node的内置模块主要用于断言。assert模块提供了一些简单的测试功能...

  • 断言NSAssert

    NSAssert与assert NSAssert和assert是断言,主要的差别是assert在断言失败的时候只是...

  • NSAssert断言

    NSAssert与assert NSAssert和assert是断言,主要的差别是assert在断言失败的时候只是...

  • Node Assert

    1. 关于Node Assert 顾名思义应该就是跑测试用例里的断言吧,里面有各种接口判断 assert() 和t...

  • node系列之assert(断言)

    地址 传送门 说明 提供了简单地断言机制,方便在测试阶段快速地发现并定位问题。需要注意的是,这并不是一个测试框架,...

  • Node文档笔记-assert断言

    assert 模块 提供了断言测试的函数,用于测试不变式。一直觉得最直观的学习模式是首先抛出demo,然后通过对d...

  • nodejs api

    asset 断言 assert模块提供了一组简单的断言测试集合,用于测试不变量。 该模块是供 Node.js 内部...

  • maven tests

    (一)关于断言assert 断言assert与异常exception的区别在于:1、断言用于调试代码2、异常用于捕...

网友评论

      本文标题:【node】assert 断言

      本文链接:https://www.haomeiwen.com/subject/tvvxkctx.html