NodeJS—Assert使用樣本

來源:互聯網
上載者:User

標籤:sse   throw   function   func   style   als   err   not   出錯   

NodeJs中Assert斷言庫的使用樣本:

 

從中可以找到一些規律:

  • equal、notEqual、deepEqual、notDeepEqual:使用‘==‘比較符進行比較。即:不含strict的方法,使用的都是‘==‘比較符。
  • strictEqual、notStrictEqual、strictDeepEqual、notStrictDeepEqual:使用‘===‘比較符進行比較。即:含strict的方法,使用的都是‘===‘比較符。

 

describe(‘Assert用法測試‘, function () {    var obj1 = {        name: ‘111‘,        parent: {            mother: ‘222‘        }    }    var obj2 = {        name: ‘111‘,        parent: {            mother: ‘222‘        }    }    var obj3 = {        name: ‘333‘    }    var obj4 = {        name: 333    }    it(‘assert.ok‘, function () {        var msg = ‘值應為真‘        assert.ok(true, msg)        assert.ok(‘1‘, msg)        assert.ok(1, msg)    });    it(‘assert.equal‘, function () {        assert.equal(1, ‘1‘, ‘兩個值應相等。使用\‘==\‘進行比較‘)    })    it(‘assert.notEqual‘, function () {        assert.notEqual(1, 2, ‘兩個值應不相等。使用\‘!=\‘進行比較‘)    })    it(‘assert.deepEqual‘, function () {        assert.deepEqual(obj1, obj2, ‘兩個對象中的屬性應深度全等。使用\‘==\‘進行比較‘)    })    it(‘assert.notDeepEqual‘, function () {        assert.notDeepEqual(obj1, obj3, ‘兩個對象中的屬性應深度不全等。使用\‘!=\‘進行比較‘)    })    it(‘assert.strictEqual‘, function () {        var msg = ‘兩個值應相等。使用\‘===\‘進行比較‘        assert.strictEqual(1, 1, msg)        assert.strictEqual(‘1‘, ‘1‘, msg)        assert.strictEqual(true, true, msg)    })    it(‘assert.notStrictEqual‘, function () {        var msg = ‘兩個值應不相等,或值為不同類型。使用\‘!==\‘進行比較‘        assert.notStrictEqual(1, ‘1‘, msg)        assert.notStrictEqual(1, 2, msg)    })    it(‘assert.deepStrictEqual‘, function () {        assert.deepStrictEqual(obj1, obj2, ‘兩個對象中的屬性應深度全等,並且對應的值為同一類型。使用\‘===\‘進行比較‘)    })    it(‘assert.notDeepStrictEqual‘, function () {        assert.notDeepStrictEqual(obj4, obj3, ‘兩個對象中的屬性應深度不全等,或對應的值為不同類型。使用\‘!==\‘進行比較‘)    })    it(‘assert.throws‘, function () {        // 方法中應當拋出對應錯誤資訊        assert.throws(function () {            throw ‘錯誤資訊‘        }, /錯誤資訊/)    })    it(‘assert.doesNotThrow‘, function () {        // 方法中不應拋出對應錯誤        assert.doesNotThrow(function () {            var a = 1 + 1;        }, RangeError, ‘拋出了RangeError錯誤‘)    })    it(‘assert.ifError‘, function () {        // 參數為真是,拋出錯誤;為假時,測試通過        assert.ifError(false)    })    // it(‘assert.fail‘, function () {    //     assert.fail(1, ‘1‘, ‘主動拋出錯誤‘)    // })})

 

NodeJS—Assert使用樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.