在调用模拟的firebase函数之前,jest测试结束,因此失败

时间:2017-08-05 09:55:44

标签: firebase react-native redux jestjs redux-thunk

我正在尝试使用jest来测试对firebase的调用。 Firebase是Google提供的在线数据库服务。我正在嘲笑firebase模块,如下所示

'use strict';

const firebase = jest.genMockFromModule('firebase');

const ref = jest.fn(() => {
  return {
    child: jest.fn(() => {
      return ref
    }),
    update: jest.fn(() => {
      console.log('Called update')
      return Promise.resolve()
    })
  }
})

firebase.initializeApp = jest.fn()
firebase.database = jest.fn(() => {
  return {
    ref: ref
  }
})

module.exports = firebase

我只是嘲笑我目前需要测试的功能。以下是我的测试用例。

it('+++ actionCreator addAlarm', () => {
    const store = mockStore(initialState)
    store.dispatch(ActionCreators.addAlarm(alarm));
    // Make sure that the scheduleNotifications API is called
    expect(scheduleNotifications).toHaveBeenCalled();
    expect(firebase.initializeApp).toHaveBeenCalled();
    expect(firebase.database().ref().update).toHaveBeenCalled();
});

测试用例中的最后一行是尝试确保我正在调用被模拟的firebase update函数。

以下是控制台输出

abcs-MBP-2:GalarmApp abc$ npm test

> GalarmApp@1.0.53 test /Users/abc/Projects/GalarmApp
> jest

 FAIL  __tests__/actionsSpecs.js
  ● >>>A C T I O N --- Test galarm actions:  › +++ actionCreator addAlarm

    expect(jest.fn()).toHaveBeenCalled()

    Expected mock function to have been called.

      at Object.<anonymous> (__tests__/actionsSpecs.js:58:52)
      at tryCallTwo (node_modules/promise/lib/core.js:45:5)
      at doResolve (node_modules/promise/lib/core.js:200:13)
      at new Promise (node_modules/promise/lib/core.js:66:3)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
      at tryCallOne (node_modules/promise/lib/core.js:37:12)
      at node_modules/promise/lib/core.js:123:15

  >>>A C T I O N --- Test galarm actions:
    ✕ +++ actionCreator addAlarm (8ms)
    ✓ +++ actionCreator setConnectionStatus (4ms)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   1 passed, 1 total
Time:        1.989s, estimated 2s
Ran all test suites.
  console.warn node_modules/rn-host-detect/index.js:45
    [SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()

  console.log __mocks__/firebase.js:11
    Called update

测试用例在我检查调用update函数的行上失败了。如果向下看控制台输出,您将看到Called update控制台存在,这意味着调用了更新函数,但在测试用例失败后调用它。

这是addAlarm动作,它是一个thunk动作

const addAlarm = (alarm) =>  (dispatch, getState) => {
  if(alarm.status) {
    NotificationManager.scheduleNotifications(alarm);
  }

  const alarmObjForFirebase = this.createAlarmObjForFirebase(alarm) 
  firebaseRef.update(alarmObjForFirebase)
}

根据我的理解,对firebase update函数的调用并非异步发生。

如果您有关于我如何解决此问题的建议,请告诉我。

1 个答案:

答案 0 :(得分:3)

我在代码中发现了问题并将其作为解决方案发布给其他人。问题是定义update模拟的方式。它的定义方式是,每次我调用update update`函数时,我都会获得firebase.database().ref().update. Since this is a new instance of the mock function, it wouldn't contain any data about模拟函数的新实例。

我需要更改代码如下

const update = jest.fn(() => {
  return Promise.resolve()
})

const ref = jest.fn(() => {
  return {
    update: update
  }
})

这样,我没有创建update模拟函数的新实例,并且我能断言它在测试用例期间被调用过。