如何模拟/ stub react-native-firebase?

时间:2018-06-14 08:36:36

标签: firebase react-native testing jestjs react-native-firebase

有谁知道为什么这不起作用?

我的api电话:

import firebase from 'react-native-firebase';

export default function getAuth() {
  return new Promise((resolve) => {
    firebase.auth().onAuthStateChanged((user) => {
      resolve(user);
    });
  });
}

我的单元测试失败,"预期有一个断言要被调用但是接到零断言调用"。

import firebase from 'react-native-firebase';

import getAuth from '../'; // eslint-disable-line

jest.mock('react-native-firebase', () => {
  return {
    auth: () => {
      return {
        onAuthStateChanged: () => {
          return Promise.resolve({
            name: 'Shaun',
          });
        },
      };
    },
  };
});

it('resolves a promise containing the user', async () => {
  expect.assertions(1);
  const response = await getAuth();
  expect(response).toEqual({ name: 'Shaun' });
});

1 个答案:

答案 0 :(得分:0)

  • 您返回了已解决的承诺=>之后什么都不会被召唤。
  • 您的onAuthStateChanged是一个回调函数

相反,用此替换onAuthStateChanged的工具:

onAuthStateChanged: jest.fn(cb => cb({ name: 'Shaun' }));

This can be solved using the classifier as follows