在使用 chai 进行测试期间无法将错误与抛出匹配
Cannot match error with a throw during test with chai
我正在尝试编写一个捕获错误的测试用例 "Error: Please provide pitchWidth and pitchHeight"。但是我似乎无法期望将 throw 视为成功的测试。
代码:
mocha.describe('testValidationOfBadInputData()', function() {
mocha.it('init game fails on pitch height', async() => {
let t1location = './init_config/team1.json'
let t2location = './init_config/team2.json'
let plocation = './test/input/badInput/badPitchHeight.json'
// let badFn = await validation.initGame(t1location, t2location, plocation)
expect(await validation.initGame(t1location, t2location, plocation)).to.throw()
}) })
输出:
1) testValidationOfBadInputData()
init game fails on pitch height:
Error: Please provide pitchWidth and pitchHeight
at Object.validatePitch (lib/validate.js:56:11)
at Object.initiateGame (engine.js:18:12)
at Object.initGame (test/lib/validate_tests.js:9:29)
其他尝试也失败了:
1)
expect(await validation.initGame(t1location, t2location, plocation)).to.throw(Error, 'Please provide pitchWidth and pitchHeight');
2)
expect(await validation.initGame.bind(t1location, t2location, plocation)).to.throw();
不确定我做错了什么,文档看起来也不是很明显。 https://www.chaijs.com/api/bdd/#method_throw
async function initGame(t1, t2, p) {
let team1 = await common.readFile(t1)
let team2 = await common.readFile(t2)
let pitch = await common.readFile(p)
let matchSetup = engine.initiateGame(team1, team2, pitch)
return matchSetup
}
以上是我调用的函数
我认为这看起来与我昨天遇到的问题相似并且与这个问题匹配:
Is node's assert.throws completely broken?
函数在传递给 expect() 之前在堆栈中执行。
改为尝试
expect(function() { await validation.initGame(t1location, t2location, plocation); }).to.throw()
我能够通过执行以下操作创建正确的测试:
mocha.describe('testValidationOfBadInputData()', function() {
mocha.it('init game fails on pitch height', async() => {
let t1location = './init_config/team1.json'
let t2location = './init_config/team2.json'
let plocation = './test/input/badInput/badPitchHeight.json'
try{
await validation.initGame(t1location, t2location, plocation);
}catch(err){
expect(err).to.be.an('Error');
expect(err.toString()).to.have.string('Error: Please provide pitchWidth and pitchHeight')
}
})
})
正如 Matt 所描述的,我尝试在 expect 的函数内调用函数。这需要一个异步函数(因为我正在使用 await)但随后因
而失败
UnhandledPromiseRejectionWarning: Error: Please provide pitchWidth and
pitchHeight
这让我想到将它放入 try catch 块中,然后处理我们返回的错误。
在catch块中;
预期输出是错误的
预期(错误)。to.be.an('Error');
将错误的字符串版本与预期输出相匹配
期望(err.toString()).to.have.string('Error: my error')
这可能不是最佳解决方案。很高兴接受其他答案。
我正在尝试编写一个捕获错误的测试用例 "Error: Please provide pitchWidth and pitchHeight"。但是我似乎无法期望将 throw 视为成功的测试。
代码:
mocha.describe('testValidationOfBadInputData()', function() { mocha.it('init game fails on pitch height', async() => { let t1location = './init_config/team1.json' let t2location = './init_config/team2.json' let plocation = './test/input/badInput/badPitchHeight.json' // let badFn = await validation.initGame(t1location, t2location, plocation) expect(await validation.initGame(t1location, t2location, plocation)).to.throw() }) })
输出:
1) testValidationOfBadInputData()
init game fails on pitch height:
Error: Please provide pitchWidth and pitchHeight
at Object.validatePitch (lib/validate.js:56:11)
at Object.initiateGame (engine.js:18:12)
at Object.initGame (test/lib/validate_tests.js:9:29)
其他尝试也失败了:
1)
expect(await validation.initGame(t1location, t2location, plocation)).to.throw(Error, 'Please provide pitchWidth and pitchHeight');
2)
expect(await validation.initGame.bind(t1location, t2location, plocation)).to.throw();
不确定我做错了什么,文档看起来也不是很明显。 https://www.chaijs.com/api/bdd/#method_throw
async function initGame(t1, t2, p) { let team1 = await common.readFile(t1) let team2 = await common.readFile(t2) let pitch = await common.readFile(p) let matchSetup = engine.initiateGame(team1, team2, pitch) return matchSetup }
以上是我调用的函数
我认为这看起来与我昨天遇到的问题相似并且与这个问题匹配: Is node's assert.throws completely broken?
函数在传递给 expect() 之前在堆栈中执行。
改为尝试
expect(function() { await validation.initGame(t1location, t2location, plocation); }).to.throw()
我能够通过执行以下操作创建正确的测试:
mocha.describe('testValidationOfBadInputData()', function() {
mocha.it('init game fails on pitch height', async() => {
let t1location = './init_config/team1.json'
let t2location = './init_config/team2.json'
let plocation = './test/input/badInput/badPitchHeight.json'
try{
await validation.initGame(t1location, t2location, plocation);
}catch(err){
expect(err).to.be.an('Error');
expect(err.toString()).to.have.string('Error: Please provide pitchWidth and pitchHeight')
}
})
})
正如 Matt 所描述的,我尝试在 expect 的函数内调用函数。这需要一个异步函数(因为我正在使用 await)但随后因
而失败UnhandledPromiseRejectionWarning: Error: Please provide pitchWidth and pitchHeight
这让我想到将它放入 try catch 块中,然后处理我们返回的错误。
在catch块中;
预期输出是错误的
预期(错误)。to.be.an('Error');
将错误的字符串版本与预期输出相匹配
期望(err.toString()).to.have.string('Error: my error')
这可能不是最佳解决方案。很高兴接受其他答案。