第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Jest Mocks 在調(diào)用 Mocks 時(shí)未在 expect().toBeCalled()

Jest Mocks 在調(diào)用 Mocks 時(shí)未在 expect().toBeCalled()

米琪卡哇伊 2023-03-24 15:28:05
對(duì)于有問(wèn)題的測(cè)試,我模擬了一些回調(diào)并將它們傳遞給我正在測(cè)試的函數(shù)。我在模擬中添加了 console.log 只是為了嘗試調(diào)試正在發(fā)生的事情。這些 console.log 正在測(cè)試日志中打印出來(lái),因此看起來(lái)好像模擬回調(diào)實(shí)際上在測(cè)試期間被正確調(diào)用(請(qǐng)參閱下面的測(cè)試輸出)但是當(dāng)我執(zhí)行 expect(mockedFunction).toBeCalled() 時(shí)斷言失敗。我不明白為什么它會(huì)失敗,因?yàn)槟M回調(diào)在測(cè)試運(yùn)行時(shí)注銷(xiāo)到控制臺(tái)。這是我的代碼:這是我要測(cè)試的代碼。import IAccount from './IAccount';import IAccountManager from './IAccountManager';import firebase from '../firebase/Firebase';import { stringHasASymbol } from '../../common/Utility';export class FirebaseAccountManager implements IAccountManager {  register(newAccount: IAccount, successCallback: (response: any) => any, errorCallback: (error: any) => any): void {    console.log("called: FirebaseAccountManager:register()");    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 })      });  }  private convertRegisterErrorCode(code: string): string {    if (code === 'auth/email-already-in-use') {      return 'email-already-in-use';    }    return 'unsupported-error-type: firebase error code = ' + stringHasASymbol;  }}這是我的測(cè)試:import { FirebaseAccountManager } from './FirebaseAccountManager';import IAccount from './IAccount';jest.mock('firebase/app', () => (  {    auth: jest.fn().mockReturnThis(),    initializeApp: jest.fn(),    createUserWithEmailAndPassword: jest.fn()      .mockResolvedValueOnce(true)      .mockRejectedValueOnce({        code: 'invalid-email'      })  }));const mockSuccessCallback = jest.fn((response: any) => {  console.log("MOCK SUCCESS CALLBACK CALLED", response);  return 'Success!';});const mockErrorCallback = jest.fn((error: any) => {  console.log("MOCK ERROR CALLBACK CALLED", error);  return { code: 'invalid-email', message: 'this email is already in use' }});afterEach(() => {  jest.clearAllMocks();});});
查看完整描述

1 回答

?
慕田峪7331174

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")

  });


查看完整回答
反對(duì) 回復(fù) 2023-03-24
  • 1 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)