簡體   English   中英

摩卡 - 如果它是正確的,測試也會失敗

[英]Mocha - Test fails also if it is correct

這是測試代碼:

chai.assert.throws(() => {
            host1.add()
        }, TriedAddingDuplicateError)

如您所見,已拋出錯誤,Mocha 得到它,但測試仍標記為失敗:

AssertionError: expected [Function] to throw 'TriedAddingDuplicateError' but 
'TriedAddingDuplicateError' was thrown
  • 注1:可以看到,報錯的方法是實例方法
  • 注意2:我確實嘗試使用另一種方式chai.expect(() => host1.add()).to.throw(TriedAddingDuplicateError) ,但結果是一樣的

你知道怎么修嗎?

我缺少實例化 function 中的錯誤 class:
之前的host.js

add () {
    let database = db.read()

    const hostToAdd = Host.findByHost(this.host)

    if (Validation.IsEmptyObject(hostToAdd)) {
        database.hosts.push(this)
        db.write(database)

        return this
    }

    throw TriedAddingDuplicateError
}

host.js 現在

add () {
    let database = db.read()

    const hostToAdd = Host.findByHost(this.host)

    if (Validation.IsEmptyObject(hostToAdd)) {
        database.hosts.push(this)
        db.write(database)

        return this
    }

    throw new TriedAddingDuplicateError()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM