如何使用排毒模拟@ react-native-community / geolocation?

时间:2019-10-25 06:38:26

标签: react-native geolocation detox

描述错误

与地理定位模块一起使用时,排毒测试崩溃

错误

Error: @react-native-community/geolocation: NativeModule.RNCGeolocation is null. To fix this issue try these steps:
• Run `react-native link @react-native-community/geolocation` in the project root.
• Rebuild and re-run the app.
• If you are using CocoaPods on iOS, run `pod install` in the `ios` directory and then rebuild and re-run the app. You may also need to re-open Xcode to get the new pods.
If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-community/react-native-geolocation

<global>
    nativeInterface.js:17:8
loadModuleImplementation
    require.js:322:6
<global>
    implementation.native.js:11
loadModuleImplementation
    require.js:322:6
<global>
    index.js:11
loadModuleImplementation
    require.js:322:6
<global>
    TravelTimeContainer.js:3
loadModuleImplementation
    require.js:322:6
<global>
    index.js:1
loadModuleImplementation
    require.js:322:6
<global>
    MyDay.js:5
loadModuleImplementation
    require.js:322:6
<global>
    BottomTabNavigator.js:14
loadModuleImplementation
    require.js:322:6
<global>
    AppNavigator.js:16
loadModuleImplementation
    require.js:322:6
<global>
    Setup.js:8
loadModuleImplementation
    require.js:322:6
<global>
    App.js:2
loadModuleImplementation
    require.js:322:6
<global>
    index.js:6
loadModuleImplementation
    require.js:322:6
guardedLoadModule
    require.js:201:45
global code
    index.bundle?platform=ios&dev=true&minify=false:195242:4

环境(请填写以下信息):

  • “ react-native”:“ 0.61.1”,
  • “反应”:“ ^ 16.9.0”,
  • “ @ react-native-community /地理位置”:“ ^ 2.0.2”,
  • “排毒”:“ ^ 14.4.1”,

注意

  • 我正在iOS模拟器上运行测试。
  • 地理位置数据库已正确链接,并且可以在运行时正常运行。

请提供解决方案。

1 个答案:

答案 0 :(得分:0)

有一个对我有用的解决方案,发布在github上:

https://github.com/react-native-community/react-native-geolocation/issues/44#issuecomment-512720306

嘿@SergejDK,您需要模拟RNCGeolocation本机模块。在根级别上创建一个文件夹__mocks__。在已创建的文件夹中创建另一个文件夹@react-native-community,并在该文件夹中创建一个名为geolocation.js的文件,您必须在其中指定模拟:

export default {
  addListener: jest.fn(),
  getCurrentPosition: jest.fn(),
  removeListeners: jest.fn(),
  requestAuthorization: jest.fn(),
  setConfiguration: jest.fn(),
  startObserving: jest.fn(),
  stopObserving: jest.fn()
};

文件夹结构应类似于:

.
├── __mocks__/
│   │── @react-native-community/ 
│       | -- geolocation.js