Nodejs斷言測試

來源:互聯網
上載者:User

標籤:

var assert = require(‘assert‘);

/*node中,我們可以使用assert模組來測試代碼。
equal()和notEqual()分別作相等性和不等性的判斷,
第一個參數是期望值,第二個參數是真實值,第三個參數是異常資訊*/

// assert.equal(1,2,[console.log(‘ok‘)]);//期望值和真實值無論相等還是不相等,輸出資訊,為什嗎?
// assert.equal(1,2,[‘not equal‘]);//期望值和真實值不相等,拋出異常資訊

/*ok()方法是比較真值的簡潔方法,相當於是用==比較當前值是否為true。*/

// assert.ok(‘‘,‘w‘);//空格為false
// assert.ok(‘This is a string‘, ‘Strings that are not empty are truthy‘); //true
 assert.ok(0, ‘Zero is not truthy‘);//0為false

// assert.fale(1,2,‘not equal‘,console.log(‘error‘));

/*node提供了對object對象的比較方法deepEqual() 和 notDeepEqual(),
他們採用下面的步驟比較對象,有一個步驟不匹配就拋出異常:
1.採用===比較;
2.比較他們是否是Buffers,如果是則比較長度,接下來每位元組每位元組的比較;
3.是用==比較;4.最後如果參數是object對象,則比較他們得屬性長度和屬性值。
可以看的出來,這兩個方法效能上可能要差些,所以只有在需要的時候才使用他們。*/

// assert.deepEqual(1,2,[‘not equal‘]);

/*assert.throws(block[, error][, message])
聲明一個 block 用來拋出錯誤(error), error可以是建構函式,Regex或其他驗證器。*/

// assert.throws(function(){
//   throw new Error(‘message is wrong‘);
// });
// assert.doesNotThrow(function() {
//   throw new Error("I lived in the ocean way before Nemo");
// });

Nodejs斷言測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.