如何用排毒模拟@react-native-community/geolocation?
Posted
技术标签:
【中文标题】如何用排毒模拟@react-native-community/geolocation?【英文标题】:How to mock @react-native-community/geolocation with detox? 【发布时间】:2020-02-21 12:07:25 【问题描述】:描述错误
与地理定位模块一起使用时排毒测试崩溃
错误
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/geolocation”:“^2.0.2”, “排毒”:“^14.4.1”,注意
我正在 iOS 模拟器上运行测试。 Geolocation 库已正确链接并在运行时正常工作。请提供解决方案。
【问题讨论】:
请提供您正在使用且存在可重现错误的测试代码。 您可以只添加带有 detox 的地理定位库,并且不会运行任何测试。 你找到了什么解决方案? 【参考方案1】:有一个对我有用的解决方案已发布在 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
【讨论】:
以上是关于如何用排毒模拟@react-native-community/geolocation?的主要内容,如果未能解决你的问题,请参考以下文章