Chai 将错误记录到控制台而不是让测试失败
Chai logs error into console instead of failing the test
我有以下测试代码,用于验证在调用一个 returns 承诺的函数后,生成的数组与存根数组相同。
it('Should return locations', () => {
const result = loginSvc.getLocations()
expect(result).to.eventually.eql(['location1', 'asd', 'location3', 'location4', 'location5'])
})
loginSvc.getLocations()
只是一个被模拟的函数,returns 是一个数组:['location1', 'location2', 'location3', 'location4', 'location5']
当我 运行 测试时,它没有像应该的那样失败,甚至作为误报成功了,在控制台中我得到了这个:
ERROR LOG: 'Unhandled promise rejection', AssertionError{message: 'expected [ Array(5) ] to have the same members as [ Array(5) ]', showDiff: true, actual: ['location1', 'location2', 'location3', 'location4', 'location5'], expected: ['location1', 'asd', 'location3', 'location4', 'location5'], stack: 'AssertionError@http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:9320:24
assert@http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:239:31
somethingMethod@http://localhost:9000/base/node_modules/chai-things/lib/chai-things.js?da5f13ef7d7d30f512b1cd8c3a12b3ed43cd7d31:97:30
overwritingMethodWrapper@http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:8932:38
allMethod@http://localhost:9000/base/node_modules/chai-things/lib/chai-things.js?da5f13ef7d7d30f512b1cd8c3a12b3ed43cd7d31:165:30
overwritingMethodWrapper@http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:8932:38
http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:3379:16
methodWrapper@http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:7709:30
http://localhost:9000/base/node_modules/chai-as-promised/lib/chai-as-promised.js?ac71de40b7ca85a0488f7d3c971a22ddd0e149a8:308:31
run@http://localhost:9000/base/spec.js?20bf9e1ddf32e8fc2bfe38226be11b7e65336abf:72447:29
http://localhost:9000/base/spec.js?20bf9e1ddf32e8fc2bfe38226be11b7e65336abf:72460:33
flush@http://localhost:9000/base/spec.js?20bf9e1ddf32e8fc2bfe38226be11b7e65336abf:72685:11', line: 243, sourceURL: 'http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae'}
但是测试通过了
我相信我找到了解决方案,或者更确切地说是解决方法:
it('Should return locations', done => {
loginSvc.getLocations()
.then(locations => {
expect(locations ).to.eql(['location1', 'asd', 'location3', 'location4', 'location5'])
done()
})
.catch(done)
})
我有以下测试代码,用于验证在调用一个 returns 承诺的函数后,生成的数组与存根数组相同。
it('Should return locations', () => {
const result = loginSvc.getLocations()
expect(result).to.eventually.eql(['location1', 'asd', 'location3', 'location4', 'location5'])
})
loginSvc.getLocations()
只是一个被模拟的函数,returns 是一个数组:['location1', 'location2', 'location3', 'location4', 'location5']
当我 运行 测试时,它没有像应该的那样失败,甚至作为误报成功了,在控制台中我得到了这个:
ERROR LOG: 'Unhandled promise rejection', AssertionError{message: 'expected [ Array(5) ] to have the same members as [ Array(5) ]', showDiff: true, actual: ['location1', 'location2', 'location3', 'location4', 'location5'], expected: ['location1', 'asd', 'location3', 'location4', 'location5'], stack: 'AssertionError@http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:9320:24 assert@http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:239:31 somethingMethod@http://localhost:9000/base/node_modules/chai-things/lib/chai-things.js?da5f13ef7d7d30f512b1cd8c3a12b3ed43cd7d31:97:30 overwritingMethodWrapper@http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:8932:38 allMethod@http://localhost:9000/base/node_modules/chai-things/lib/chai-things.js?da5f13ef7d7d30f512b1cd8c3a12b3ed43cd7d31:165:30 overwritingMethodWrapper@http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:8932:38 http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:3379:16 methodWrapper@http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:7709:30 http://localhost:9000/base/node_modules/chai-as-promised/lib/chai-as-promised.js?ac71de40b7ca85a0488f7d3c971a22ddd0e149a8:308:31 run@http://localhost:9000/base/spec.js?20bf9e1ddf32e8fc2bfe38226be11b7e65336abf:72447:29 http://localhost:9000/base/spec.js?20bf9e1ddf32e8fc2bfe38226be11b7e65336abf:72460:33 flush@http://localhost:9000/base/spec.js?20bf9e1ddf32e8fc2bfe38226be11b7e65336abf:72685:11', line: 243, sourceURL: 'http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae'}
但是测试通过了
我相信我找到了解决方案,或者更确切地说是解决方法:
it('Should return locations', done => {
loginSvc.getLocations()
.then(locations => {
expect(locations ).to.eql(['location1', 'asd', 'location3', 'location4', 'location5'])
done()
})
.catch(done)
})