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: '[email protected]', 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: '[email protected]', 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")
});
添加回答
舉報