我正在尝试以开玩笑的方式测试Firebase云消息传递,但我的模拟功能有错误...
如何测试所有函数调用?
expect(jest.fn()).toHaveBeenCalled()
Expected mock function to have been called.
import { Sentry, SentrySeverity } from 'react-native-sentry';
import RNFirebase from 'react-native-firebase';
import Veery from 'react-native-veery';
export default function firebaseCloudMessaging() {
try {
const firebase = RNFirebase.app().messaging();
firebase.getToken().then(Veery.setVeeryToken);
firebase.getInitialNotification();
firebase.onMessage(Veery.VeeryNotificationHandler);
firebase.onTokenRefresh(Veery.setVeeryToken);
} catch (error) {
Sentry.captureException(error, { level: SentrySeverity.Error });
console.warn('Error with firebaseCloudMessaging ', error);
}
}
我的测试: https://gist.github.com/ludwigCDSBDX/82749eb20eac44fffac8d5318864eb6f
谢谢!
答案 0 :(得分:1)
我的错误: 先前的模拟创建了多个模拟实例 因此,当我尝试检查是否已调用模拟程序时,我在比较的是相同的函数,而不是相同的函数实例!
jest.mock('react-native-firebase', () => {
const app = { messaging: () => messaging };
const messaging = {
getToken: jest.fn(() => Promise.resolve('myTokenMock')),
getInitialNotification: jest.fn(),
onMessage: jest.fn(),
onTokenRefresh: jest.fn(() => Promise.resolve('myMockTokenRefresh')),
};
return { app: () => app };
});