async 函数的 jasmine expectedAsync 不起作用
jasmine expectedAsync for async function doesn't work
我正在运行进行以下测试:
it("should get rejected", async done => {
class someTest {
async run(){
return this.rejectFunc();
}
async rejectFunc(){
return new Promise( (_,reject)=>{
setTimeout(()=>{
reject()
},300)
})
}
}
const test = new someTest();
spyOn(test, 'rejectFunc').and.callThrough();
test.run()
await expectAsync(test.rejectFunc).toBeRejected();
done();
});
但是测试超时失败:
Error: Timeout - Async callback was not invoked within timeout
specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
我做错了什么?我想 运行 run
方法并验证 rejectFunc
是否被拒绝。
这是我第一次看到 expectAsync
但它期望接受一个承诺而不是函数的名称。
所以试试这个:
// call the function with ()
await expectAsync(test.rejectFunc()).toBeRejected();
另一种测试方法是:
try {
await test.rejectFunc();
} catch (error) {
// call done here, meaning promise was rejected and therefore we can assert
// it was rejected
done();
}
编辑
const test = new someTest();
spyOn(test, 'rejectFunc').and.callThrough();
try {
await test.run();
} catch (e) {
expect(test.rejectFunc).toHaveBeenCalled();
}
done();
我正在运行进行以下测试:
it("should get rejected", async done => {
class someTest {
async run(){
return this.rejectFunc();
}
async rejectFunc(){
return new Promise( (_,reject)=>{
setTimeout(()=>{
reject()
},300)
})
}
}
const test = new someTest();
spyOn(test, 'rejectFunc').and.callThrough();
test.run()
await expectAsync(test.rejectFunc).toBeRejected();
done();
});
但是测试超时失败:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
我做错了什么?我想 运行 run
方法并验证 rejectFunc
是否被拒绝。
这是我第一次看到 expectAsync
但它期望接受一个承诺而不是函数的名称。
所以试试这个:
// call the function with ()
await expectAsync(test.rejectFunc()).toBeRejected();
另一种测试方法是:
try {
await test.rejectFunc();
} catch (error) {
// call done here, meaning promise was rejected and therefore we can assert
// it was rejected
done();
}
编辑
const test = new someTest();
spyOn(test, 'rejectFunc').and.callThrough();
try {
await test.run();
} catch (e) {
expect(test.rejectFunc).toHaveBeenCalled();
}
done();