您好,登錄后才能下訂單哦!
本文實例講述了Node.js assert斷言原理與用法。分享給大家供大家參考,具體如下:
node.js官方API中文版 http://nodeapi.ucdok.com/#/api/assert.html
assert 模塊主要用于編寫程序的單元測試時使用,通過斷言可以提早發(fā)現(xiàn)和排查出錯誤。
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是個函數(shù),函數(shù)名為ok。javascript中函數(shù)是Function類的實例,也就是對象,所以可為其添加fail和equal等屬性。注意輸出結(jié)果第9行 ok:[Circular] 這個表述,這是指針循環(huán)的意思,即ok屬性指向了本身,所以調(diào)用assert.ok就相當(dāng)于調(diào)用了assert本身。
測試如下:
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] }
比較相等操作符 ‘==' 會根據(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
全等操作符 ‘===' 會先比較元素的類型,只有類型和值都一樣才算相等。
true === 1; // false 1 === '1'; // false
轉(zhuǎn)回正題:
注意:如果不設(shè)置message,就會將value打印出來。
assert.fail(actual, expected, message, operator)
在不檢查任何條件的情況下使斷言失敗。如果有錯誤信息則輸出錯誤信息,否則輸出actual和expected,中間用operator隔開。
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)兩邊都是基本變量的時候轉(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)格相等,和全等符號(===)的判斷結(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)比較的雙方均為基本類型時,等價于euqal()
。
當(dāng)比較的雙方均為引用類型時,即將引用類型中的每一個屬性用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:
注意:錯誤信息不能是一個字符串。如果字符串被作為第二個參數(shù),那么錯誤就會被假定為省略,并且字符串將會被用作提示信息,這樣很容易導(dǎo)致錯誤。
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)值為真時,拋出AssertionError錯誤。該方法在測試回調(diào)函數(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
希望本文所述對大家nodejs程序設(shè)計有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。