Sinon 和 Mocha:如何在测试功能断言之前等待承诺得到解决?
Sinon and Mocha: How to await for a promised to be resolved before assertion in test function?
我有一个文件,
function fetchDevices () {
device.findAll()
.then(allDevices =>
console.log("Fetched for DB")
)
}
在测试文件中我模拟了device
。现在我想要 know/await 只要这个 findAll()
returns 一个承诺,然后在测试函数中继续断言。我已经尝试了很多东西,但 setTimeout
不是我想要的。
我不能存根 devices
因为我已经用另一个库模拟它,这省去了模拟或存根属性的很多麻烦。
非常感谢您的帮助。
如果你在 it 上声明回调函数,你就可以使用 await( async, something like this:
it('blablabla', async () => {
await asyncFunction();
});
变化:
function fetchDevices () {
device.findAll()
.then(allDevices =>
console.log("Fetched for DB")
)
}
至:
function fetchDevices () {
return device.findAll()
.then(allDevices =>
console.log("Fetched for DB")
)
}
现在 fetchDevices
returns 一个承诺,你可以 then
它。
我有一个文件,
function fetchDevices () {
device.findAll()
.then(allDevices =>
console.log("Fetched for DB")
)
}
在测试文件中我模拟了device
。现在我想要 know/await 只要这个 findAll()
returns 一个承诺,然后在测试函数中继续断言。我已经尝试了很多东西,但 setTimeout
不是我想要的。
我不能存根 devices
因为我已经用另一个库模拟它,这省去了模拟或存根属性的很多麻烦。
非常感谢您的帮助。
如果你在 it 上声明回调函数,你就可以使用 await( async, something like this:
it('blablabla', async () => {
await asyncFunction();
});
变化:
function fetchDevices () {
device.findAll()
.then(allDevices =>
console.log("Fetched for DB")
)
}
至:
function fetchDevices () {
return device.findAll()
.then(allDevices =>
console.log("Fetched for DB")
)
}
现在 fetchDevices
returns 一个承诺,你可以 then
它。