1 回答
TA贡献1828条经验 获得超13个赞
我解决了这个。问题是 FirebaseAccountManager 中的注册函数正在处理一个承诺,但不是异步的。一旦我将异步添加到函数并在测试中等待它,测试就通过了。我认为测试断言在承诺解决或拒绝它之前调用了回调。更新代码示例如下:
async register(newAccount: IAccount, successCallback: (response: any) => any, errorCallback: (error: any) => any): Promise<any> {
console.log("called: FirebaseAccountManager:register()");
await firebase.register(newAccount.email, newAccount.password, newAccount.firstName + " " + newAccount.lastName)
.then(response => {
console.log("GOT HERE 1", response)
successCallback(true);
})
.catch(error => {
console.log("GOT HERE 2", error)
errorCallback({ code: this.convertRegisterErrorCode(error.code), message: error.message })
});
}
这是现在通过的更改测试。
test('Successful Registration', async () => {
console.log("START Successful Registration")
const newAccount: IAccount = { firstName: 'asdf', lastName: 'asdf', email: 'asdf@adf.com', password: 'qwer', phoneNumber: '', workStatus: '', city: '', postalCode: '', country: '' }
const fam = new FirebaseAccountManager();
await fam.register(newAccount, mockSuccessCallback, mockErrorCallback);
expect(mockSuccessCallback).toBeCalled();
expect(mockErrorCallback).not.toBeCalled();
console.log("DONE Successful Registration")
});
test('Failed Registration', async () => {
console.log("START Failed Registration")
const newAccount: IAccount = { firstName: 'asdf', lastName: 'asdf', email: 'asdf@adf.com', password: 'qwer', phoneNumber: '', workStatus: '', city: '', postalCode: '', country: '' }
const fam = new FirebaseAccountManager();
await fam.register(newAccount, mockSuccessCallback, mockErrorCallback);
expect(mockSuccessCallback).not.toBeCalled();
expect(mockErrorCallback).toBeCalled();
console.log("DONE Failed Registration")
});
添加回答
举报