【发布时间】:2018-04-06 21:09:01
【问题描述】:
我对 jest 单元测试很陌生,所以像模拟模块这样的东西会让人困惑。
在 react native 中,有一个组件使用 firebase 数据库从给定的 ref 返回数据:
// when the data of the current user is available
userRef.child(user.uid).on('value', snap => {
// check of val() consists data
if (snap.val()) {
let
authUser = snap.val(),
isPatient = authUser.type === "Patient",
// We update the state object so that the component is re-rendered
this.setState({
// the ID of the current user
authUserUID: user.uid,
// the type of the current user (i.e. Doctor or Patient)
authUserType: snap.val().type,
// title of the dashboard based on the current user type
activeTitle: snap.val().type === "Doctor" ? "My Patients" : "My Doctors",
});
}
});
我现在正在尝试使用此组件进行单元测试:
import 'react-native'
import React from 'react'
import renderer from 'react-test-renderer'
import LaunchScreen from "../../App/Containers/LaunchScreen";
test('LaunchScreen', () => {
const tree = renderer.create( <LaunchScreen / > ).toJSON()
expect(tree).toMatchSnapshot()
})
但它给出了一个奇怪的错误
在 iOS 上未原生找到 RNFirebase 核心模块,请确保您已将 RNFirebase pod 正确包含在您的项目中
Podfile并已运行pod install
我不知道,因为如果我运行它,firebase 确实会返回数据。那为什么说找不到RNFirebase呢?
谢谢
更新
其中一个答案给出了一个很好的分步说明,尽管它部分解决了提出的问题;错误仍然存在。例如;我现在收到这个新错误:
console.error node_modules\react-test-renderer\cjs\react-test-renderer.development.js:5530 组件出现上述错误: 在启动屏幕中
考虑在树中添加错误边界以自定义错误处理行为。
TypeError: _reactNativeFirebase2.default.app 不是函数
【问题讨论】:
-
我已经更新了我的答案。应用补丁后让我们知道结果。
-
解决了吗?如果没有,请告诉我们问题并让我们关闭它:)
标签: javascript firebase unit-testing react-native jestjs