Android: 使用 Firebase 的单元测试方法
Android: Unittest method which use Firebase
问题描述
我需要对使用 Firebase 对象的函数进行单元测试,如下面的源代码所示。
问题
编写单元测试的最佳方式是什么?
真的需要像那样对函数进行单元测试吗?
源代码
void foo() {
Firebase f = new Firebase("url").child("child1").child("child2");
if (f != null)
f.setValue(state);
}
我不熟悉 Firebase 本身,但我会为所有连接到 Firebase 的东西提取 interface
,然后我会用 Firebase 创建一个 interface
的实现(例如 FirebaseData
).仅在接口上操作,我会在不知道 Firebase 的实际实现的情况下测试我的逻辑。
测试您的代码总是值得的,因为:
- 测试证明逻辑有效
- 帮助测试regression/refactors
- 测试改进您的设计
我强烈推荐观看 J.B 的视频。雷恩斯伯格 Integrated tests are scam
在 interface
上操作还有一个很大的优势:您始终可以在不违反合同的情况下更改您的实现。
问题描述
我需要对使用 Firebase 对象的函数进行单元测试,如下面的源代码所示。
问题
编写单元测试的最佳方式是什么? 真的需要像那样对函数进行单元测试吗?
源代码
void foo() {
Firebase f = new Firebase("url").child("child1").child("child2");
if (f != null)
f.setValue(state);
}
我不熟悉 Firebase 本身,但我会为所有连接到 Firebase 的东西提取 interface
,然后我会用 Firebase 创建一个 interface
的实现(例如 FirebaseData
).仅在接口上操作,我会在不知道 Firebase 的实际实现的情况下测试我的逻辑。
测试您的代码总是值得的,因为:
- 测试证明逻辑有效
- 帮助测试regression/refactors
- 测试改进您的设计
我强烈推荐观看 J.B 的视频。雷恩斯伯格 Integrated tests are scam
在 interface
上操作还有一个很大的优势:您始终可以在不违反合同的情况下更改您的实现。