繁体   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