Mocha 和 Chai 测试预计不会抛出错误
Mocha and Chai test expect not to throw error
我正在用 Mocha 和 Chai 做一些测试
我有一些函数没有 return 值,但如果出现错误,它们会抛出错误
我想测试一下,因为如果他们不抛出错误就可以通过测试
例如
async function sum(a, b){
Promise.resolve()
.then()
if(typeof(a) !== 'number')
throw new Error('a should be a number')
if(typeof(b) !== 'number')
throw new Error('b should be a number')
console.log('the sum is ', a + b)
return
}
然后我想测试它是否正在使用类似这样的东西
expect(sum(1, 2)).not.to.throw()
解决这个问题的方法是使用 chai-as-promise
库
const chai = require('chai');
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const expect = chai.expect;
expect(sum(1, 2)).to.eventually.not.be.rejectedWith(Error)
我正在用 Mocha 和 Chai 做一些测试
我有一些函数没有 return 值,但如果出现错误,它们会抛出错误
我想测试一下,因为如果他们不抛出错误就可以通过测试
例如
async function sum(a, b){
Promise.resolve()
.then()
if(typeof(a) !== 'number')
throw new Error('a should be a number')
if(typeof(b) !== 'number')
throw new Error('b should be a number')
console.log('the sum is ', a + b)
return
}
然后我想测试它是否正在使用类似这样的东西
expect(sum(1, 2)).not.to.throw()
解决这个问题的方法是使用 chai-as-promise
库
const chai = require('chai');
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const expect = chai.expect;
expect(sum(1, 2)).to.eventually.not.be.rejectedWith(Error)