我有以下测试代码,用于验证在调用返回promise的函数后,生成的数组与存根的数组相同。
it('Should return locations', () => {
const result = loginSvc.getLocations()
expect(result).to.eventually.eql(['location1', 'asd', 'location3', 'location4', 'location5'])
})
loginSvc.getLocations()
只是一个被模拟的函数并返回一个数组:['location1', 'location2', 'location3', 'location4', 'location5']
当我运行测试时,它不会失败,因为它应该甚至成功作为误报,并且在控制台中我得到了这个:
错误日志:'未处理的拒绝承诺',AssertionError {message:'expected [Array(5)]与[Array(5)]具有相同的成员,showDiff:true,actual:['location1',' location2','location3','location4','location5'],预期:['location1','asd','location3','location4','location5'],堆叠:'AssertionError @ http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:9320:24 断言@ 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 运行@ 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'}
但测试通过了
答案 0 :(得分:1)
我相信我找到了解决方案,或者更确切地说是解决方法:
it('Should return locations', done => {
loginSvc.getLocations()
.then(locations => {
expect(locations ).to.eql(['location1', 'asd', 'location3', 'location4', 'location5'])
done()
})
.catch(done)
})