简体   繁体   中英

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:

AssertionError: expected [Function] to throw 'TriedAddingDuplicateError' but 
'TriedAddingDuplicateError' was thrown
  • Note1: As you can see, the method raising the error is an instance method
  • Note2: I did try to use one other way that is chai.expect(() => host1.add()).to.throw(TriedAddingDuplicateError) , but the result is the same

Do you know how to fix it?

I was missing to instanciate the error class in the function:
host.js before

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

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM