简体   繁体   English

摩卡 - 如果它是正确的,测试也会失败

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

This is the test code:这是测试代码:

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

As you can see, error have been thrown, Mocha got it, but the test is marked as failed anyway:如您所见,已抛出错误,Mocha 得到它,但测试仍标记为失败:

AssertionError: expected [Function] to throw 'TriedAddingDuplicateError' but 
'TriedAddingDuplicateError' was thrown
  • Note1: As you can see, the method raising the error is an instance method注1:可以看到,报错的方法是实例方法
  • Note2: I did try to use one other way that is chai.expect(() => host1.add()).to.throw(TriedAddingDuplicateError) , but the result is the same注意2:我确实尝试使用另一种方式chai.expect(() => host1.add()).to.throw(TriedAddingDuplicateError) ,但结果是一样的

Do you know how to fix it?你知道怎么修吗?

I was missing to instanciate the error class in the function:我缺少实例化 function 中的错误 class:
host.js before之前的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 now 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