[英]Test firebase cloud messaging with Jest
I am trying to test firebase cloud messaging with jest but I have an error with my mock function... 我正在尝试用玩笑来测试Firebase云消息传递,但是我的模拟功能有错误...
How can I test all function invocations? 如何测试所有函数调用?
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);
}
}
my test : https://gist.github.com/ludwigCDSBDX/82749eb20eac44fffac8d5318864eb6f 我的测试: https : //gist.github.com/ludwigCDSBDX/82749eb20eac44fffac8d5318864eb6f
Thank you! 谢谢!
my mistake: the previous mock creates several instances of mock so when I was trying to check if my mock had been called, I was comparing the same function, but not the same function instance ! 我的错误:先前的模拟创建了多个模拟实例,因此当我尝试检查是否已调用我的模拟时,我在比较的是同一函数,而不是同一函数实例!
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 };
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.