使用chai测试期间无法将错误与throw匹配
我正在尝试编写一个捕获错误的测试用例“错误:请提供pitchWidth和pitchHeight”。但我似乎无法将这一投射作为一次成功的测试。
码:
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();
不知道我做错了什么,文档似乎并不明显。
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()
使用chai测试期间无法将错误与throw匹配
我正在尝试编写一个捕获错误的测试用例“错误:请提供pitchWidth和pitchHeight”。但我似乎无法将这一投射作为一次成功的测试。
码:
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();
不知道我做错了什么,文档似乎并不明显。
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()