gpt4 book ai didi

unit-testing - react-native-firebase-messaging 开 Jest

转载 作者:行者123 更新时间:2023-12-04 10:56:32 26 4
gpt4 key购买 nike

我正在尝试模拟 Firebase Cloud Messaging (FCM),来自 @react-native-firebase/messaging .到目前为止,这是我的模拟:

jest.mock('@react-native-firebase/messaging', () => ({
hasPermission: jest.fn(() => Promise.resolve(true)),
subscribeToTopic: jest.fn(),
deleteToken: jest.fn(),
unsubscribeFromTopic: jest.fn(),
registerForRemoteNotifications: jest.fn(),
requestPermission: jest.fn(() => Promise.resolve(true)),
getToken: jest.fn(() => Promise.resolve('myMockToken')),
getAPNSToken: jest.fn(() => Promise.resolve('myMockToken')),
onTokenRefresh: jest.fn(() => Promise.resolve('myMockToken')),
onMessage: jest.fn(),
isRegisteredForRemoteNotifications: jest.mock(false),
isAutoInitEnabled: jest.mock(false),
}));

但是当我运行我的测试时,我得到:
   Invariant Violation: Native module cannot be null.

at invariant (packages/mobile/node_modules/invariant/invariant.js:40:15)
at RNFBNativeEventEmitter.invariant (packages/mobile/node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:36:7)
at new RNFBNativeEventEmitter (node_modules/@react-native-firebase/messaging/node_modules/@react-native-firebase/app/lib/internal/RNFBNativeEventEmitter.js:24:5)
at Object.<anonymous> (node_modules/@react-native-firebase/messaging/node_modules/@react-native-firebase/app/lib/internal/RNFBNativeEventEmitter.js:48:16)
at Object.<anonymous> (node_modules/@react-native-firebase/messaging/node_modules/@react-native-firebase/app/lib/internal/registry/nativeModule.js:21:1)

这告诉我我仍然需要模拟一些其他功能。我在这里缺少什么?

最佳答案

我发现我没有正确地 mock 。我创建了一个

__mocks__/@react-native-firebase/messaging.js. 

在那个文件中我写道:
module.exports = {
hasPermission: jest.fn(() => Promise.resolve(true)),
subscribeToTopic: jest.fn(),
unsubscribeFromTopic: jest.fn(),
requestPermission: jest.fn(() => Promise.resolve(true)),
getToken: jest.fn(() => Promise.resolve('myMockToken')),
};



另外在我的设置玩笑中,我有:
reactNative.NativeModules.RNCNetInfo = {
getCurrentState: jest.fn(() => Promise.resolve()),
addListener: jest.fn(),
removeListeners: jest.fn(),
};

关于unit-testing - react-native-firebase-messaging 开 Jest ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59144903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com