这是测试代码:
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)
,但结果是相同的
你知道怎么修吗?
我没有在函数中实例化错误类:
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()
}