Node.js assert斷言原理與用法分析
本文實(shí)例講述了Node.js assert斷言原理與用法。分享給大家供大家參考,具體如下:
node.js官方API中文版 http://nodeapi.ucdok.com/#/api/assert.html
assert 模塊主要用于編寫程序的單元測(cè)試時(shí)使用,通過(guò)斷言可以提早發(fā)現(xiàn)和排查出錯(cuò)誤。
class : assert
- assert.fail(actual, expected, message, operator)
- assert(value, message), 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, [message])
- assert.ifError(value)
console.log(assert); /* 輸出如下 { [Function: ok] AssertionError: { [Function: AssertionError] super_: { [Function: Error] captureStackTrace: [Function: captureStackTrace], stackTraceLimit: 10 } }, fail: [Function: fail], ok: [Circular], equal: [Function: equal], notEqual: [Function: notEqual], deepEqual: [Function: deepEqual], notDeepEqual: [Function: notDeepEqual], strictEqual: [Function: strictEqual], notStrictEqual: [Function: notStrictEqual], throws: [Function], doesNotThrow: [Function], ifError: [Function] } */
assert是個(gè)函數(shù),函數(shù)名為ok。javascript中函數(shù)是Function類的實(shí)例,也就是對(duì)象,所以可為其添加fail和equal等屬性。注意輸出結(jié)果第9行 ok:[Circular] 這個(gè)表述,這是指針循環(huán)的意思,即ok屬性指向了本身,所以調(diào)用assert.ok就相當(dāng)于調(diào)用了assert本身。
測(cè)試如下:
var test = function ok() { console.log('test ok'); } //輸出 undefined test.ok = test; //輸出 { [Function: ok] ok: [Circular] } test.fail = function fail() { console.log('test fail'); } //輸出 [Function: fail] console.log(test); //輸出 {[Function: ok] ok: [Circular], fail: [Function: fail] }
比較相等操作符 ‘==' 會(huì)根據(jù)前面的參數(shù)進(jìn)行類型轉(zhuǎn)換。
true == 1; // true 1 == true; // true true == 2; // false 2 == true; // false '' == false; // true false == ''; // true 1 == '1'; // true
全等操作符 ‘===' 會(huì)先比較元素的類型,只有類型和值都一樣才算相等。
true === 1; // false 1 === '1'; // false
轉(zhuǎn)回正題:
注意:如果不設(shè)置message,就會(huì)將value打印出來(lái)。
assert.fail(actual, expected, message, operator)
在不檢查任何條件的情況下使斷言失敗。如果有錯(cuò)誤信息則輸出錯(cuò)誤信息,否則輸出actual和expected,中間用operator隔開(kāi)。
assert.fail(1, 1); //輸出 AssertionError: 1 undefined 1 assert.fail(1, 1, undefined, '=='); //輸出 AssertionError: 1 == 1 assert.fail(1, 2, undefined, '>'); //輸出 AssertionError: 1 > 2 assert.fail(1, 2, 'whoops', '>'); //輸出 AssertionError: whoops
assert(value, message), assert.ok(value, [message])
assert(true, 'message'); //輸出 undefined assert(false, 'message'); //輸出 AssertionError: message assert.ok(true, 'message'); //輸出 undefined assert.ok(false, 'message'); //輸出 AssertionError: message
assert.equal(actual, expected, [message])
和比較操作符(==)的判斷結(jié)果相同。當(dāng)兩邊都是基本變量的時(shí)候轉(zhuǎn)化為同一類型的變量再進(jìn)行比較;如果是引用類型的變量,則直接比較其內(nèi)存地址。
assert.equal(1, 1, 'message'); //輸出 undefined assert.equal(1, '1', 'message'); //輸出 AssertionError: message
assert.strictEqual(actual, expected, [message])
Tests strict equality, as determined by the strict equality operator ( === )
嚴(yán)格相等,和全等符號(hào)(===)的判斷結(jié)果相同。
assert.strictEqual(1, 1, 'message'); //輸出 undefined assert.strictEqual(1, '1', 'message'); //輸出 AssertionError: message assert.strictEqual(1, '1', 'message'); //輸出 AssertionError: message
assert.deepEqual(actual, expected, [message])
當(dāng)比較的雙方均為基本類型時(shí),等價(jià)于euqal()
。
當(dāng)比較的雙方均為引用類型時(shí),即將引用類型中的每一個(gè)屬性用equal()
進(jìn)行比較。
assert.equal(1, '1'); //輸出 undefined assert.deepEqual(1, '1'); //輸出 undefined assert.strictEqual(1, '1'); //輸出 assert.strictEqual(1, '1'); assert.equal({a:1}, {a:'1'}); //輸出 AssertionError: { a: 1 } == {a: '1'} assert.deepEqual({a:1}, {a:'1'}); //輸出 undefined assert.strictEqual({a:1}, {a:'1'}); //輸出 AssertionError: { a: 1 } == {a: '1'}
assert.throws(block, [error], [message])
Expects the function block to throw an error.
If specified, error can be a constructor, RegExp, or validation function.
If specified, message will be the message provided by the AssertionError if the block fails to throw.
assert.throws( () => {}, Error ); //輸出 AssertionError: Missing expected exception (Error).. assert.throws( () => {throw new Error('Wrong value');}, Error ); //輸出 undefined assert.throws( () => {throw new Error('Wrong value');}, /Wrong/ ); //輸出 undefined assert.throws( () => {throw new Error('Wrong value');}, /wrong/ ); //輸出 Error: Wrong value assert.throws( () => {throw new Error('Wrong value');}, (err) => { if ((err instanceof Error) && /value/.test(err)) { return true; } }, 'unexpected error' ); //輸出 undefined
Note that error can not be a string. If a string is provided as the second argument, then error is assumed to be omitted and the string will be used for message instead. This can lead to easy-to-miss mistakes:
注意:錯(cuò)誤信息不能是一個(gè)字符串。如果字符串被作為第二個(gè)參數(shù),那么錯(cuò)誤就會(huì)被假定為省略,并且字符串將會(huì)被用作提示信息,這樣很容易導(dǎo)致錯(cuò)誤。
assert.throws(()=>{throw new Error('Wrong value');}, 'Wrong', 'did not throw with expected message'); //輸出 undefined assert.throws(()=>{}, 'Wrong', 'did not throw with expected message'); //輸出 AssertionError: Missing expected exception. Wrong assert.throws(()=>{}, /Wrong/, 'did not throw with expected message'); //輸出 AssertionError: Missing expected exception. did not with expected message.
assert.ifError(value)
Throws value if value is truthy. This is useful when testing the error argument in callbacks.
當(dāng)值為真時(shí),拋出AssertionError錯(cuò)誤。該方法在測(cè)試回調(diào)函數(shù)的參數(shù)時(shí)非常有用。
assert.ifError(0); //輸出 undefined assert.ifError(1); //輸出 1 assert.ifError('error'); //輸出 error assert.ifError(new Error('there maybe wrong')); //輸出 Error: there maybe wrong
希望本文所述對(duì)大家nodejs程序設(shè)計(jì)有所幫助。
相關(guān)文章
Nodejs實(shí)現(xiàn)多人同時(shí)在線移動(dòng)鼠標(biāo)的小游戲分享
這篇文章主要介紹了Nodejs實(shí)現(xiàn)多人同時(shí)在線移動(dòng)鼠標(biāo)的小游戲分享,本文給出了服務(wù)器端和客戶端代碼以及運(yùn)行方法,需要的朋友可以參考下2014-12-12詳解基于Node.js的HTTP/2 Server實(shí)踐
HTTP/2目前已經(jīng)逐漸的在各大網(wǎng)站上開(kāi)始使用,這篇文章主要介紹了詳解基于Node.js的HTTP/2 Server實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Egret引擎開(kāi)發(fā)指南之創(chuàng)建項(xiàng)目
Egret Engine(白鷺引擎)是一款使用TypeScript語(yǔ)言構(gòu)建的開(kāi)源免費(fèi)的移動(dòng)游戲引擎。白鷺引擎的核心定位是開(kāi)放,高效,優(yōu)雅。通過(guò)它,你可以快速地創(chuàng)建HTML5類型的移動(dòng)游戲,也可以將游戲項(xiàng)目編譯輸出成為目標(biāo)移動(dòng)平臺(tái)的原生游戲應(yīng)用。2014-09-09在Node.js下運(yùn)用MQTT協(xié)議實(shí)現(xiàn)即時(shí)通訊及離線推送的方法
這篇文章主要介紹了在Node.js下運(yùn)用MQTT協(xié)議實(shí)現(xiàn)即時(shí)通訊及離線推送的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01NodeJs實(shí)現(xiàn)簡(jiǎn)易WEB上傳下載服務(wù)器
這篇文章主要為大家詳細(xì)介紹了NodeJs實(shí)現(xiàn)一個(gè)簡(jiǎn)易WEB上傳下載服務(wù)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08Nodejs中使用captchapng模塊生成圖片驗(yàn)證碼
本篇文章主要介紹了Nodejs中使用captchapng模塊實(shí)現(xiàn)圖片驗(yàn)證碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05解決使用node命令提示:'node'不是內(nèi)部或外部命令,也不是可運(yùn)行的程序
最近在工作中遇到了個(gè)常見(jiàn)的問(wèn)題,分享給大家,這篇文章主要給大家介紹了關(guān)于如何解決使用node命令提示:'node'不是內(nèi)部或外部命令,也不是可運(yùn)行的程序的相關(guān)資料,需要的朋友可以參考下2023-02-02安裝node.js和npm的一些常見(jiàn)報(bào)錯(cuò)
NVM(Node?Version?Manager)是一個(gè)用于在同一機(jī)器上同時(shí)安裝并管理多個(gè)Node.js版本的工具,這篇文章主要給大家介紹了關(guān)于安裝node.js和npm的一些常見(jiàn)報(bào)錯(cuò),需要的朋友可以參考下2023-06-06