1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊
我解決了這個(gè)。問(wèn)題是 FirebaseAccountManager 中的注冊(cè)函數(shù)正在處理一個(gè)承諾,但不是異步的。一旦我將異步添加到函數(shù)并在測(cè)試中等待它,測(cè)試就通過(guò)了。我認(rèn)為測(cè)試斷言在承諾解決或拒絕它之前調(diào)用了回調(diào)。更新代碼示例如下:
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 })
});
}
這是現(xiàn)在通過(guò)的更改測(cè)試。
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")
});
添加回答
舉報(bào)