- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试为我的 native 移动应用程序编写一些单元测试,但在我的 Android 测试中遇到了障碍。具体来说,我正在努力寻找 Kotlin 版 Swift 的期望/ promise 的示例。
我找到了 Kotlin Promises 的例子,但它们似乎比需要的要复杂得多......
例如在我的iOS项目中测试login API函数:
func testLogin() {
/// Prepare for login
if CURRENT_USER != nil {
logout()
}
/// Login
let promise = expectation(description: "User is logged in.")
// 1. Given
var isSuccess: Bool = false
// 2. When
User.login(username: maleUsername, password: malePassword, success: {
isSuccess = true
promise.fulfill()
}) { (_, agreeToTerms) in
XCTFail()
}
wait(for: [promise], timeout: maxTimeOut)
// 3. Then
XCTAssertNotNil(CURRENT_USER)
XCTAssertTrue(isSuccess)
/// Logout
logout()
}
这对我来说很简单。我有一个异步方法 login
,它有两个可能的完成 block :success
和 failure
;在评估之前,我需要等待其中一个完成。为此,我在调用之前创建了一个promise,然后在两个完成 block 中fulfill该promise,然后我等待该promise在运行我的断言之前完成。
现在在 Kotlin 中,我有一个类似的测试:
private val loginFragment = LoginFragment()
@Test
fun loginTest() {
val username = ""
val password = ""
// TODO: Create Promise
loginFragment.loginViewModel
.login(username, password)
.observe(loginFragment, Observer {
loginFragment.activity?.onResult(it?.result,
onSuccess = {
// TODO: Fill Promise
},
onValidationError = {
// TODO: Fail Test
})
})
// TODO: Assertions
}
但我找不到与 swift 的 promises 等价的..
Kotlin 中有吗?如果没有,我将如何在 Kotlin 中实现我的 Swift 的 testLogin
方法的一个版本?
最佳答案
您可以使用 Kotlin 协程,例如:
@Test
fun loginTest() {
val result = runBlocking {
val loginResult = login()
loginResult
}
if (result == "Success") {
// do your work when Success
} else {
// do your work when Error
}
}
suspend fun login(): String = suspendCoroutine { continuation ->
val username = ""
val password = ""
loginFragment.loginViewModel
.login(username, password)
.observe(loginFragment, Observer {
loginFragment.activity?.onResult(it?.result,
onSuccess = {
continuation.resume("Success")
},
onValidationError = {
continuation.resume("Error") // take a look for other methods, e.g. resumeWithException(exception)
})
})
}
要使用协程,您需要将下一行添加到应用程序的 build.gradle 文件依赖项中:
final KOTLIN_COROUTINES_VERSION = '1.0.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$KOTLIN_COROUTINES_VERSION"
希望对您有所帮助。
关于swift - Kotlin 等价于 Swift 期望/ promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53291051/
是: x -= y; 相当于: x = x - y; 最佳答案 不,它们并不等同于您表达它们的方式。 short x = 0, y = 0; x -= y; // This compiles f
这个问题在这里已经有了答案: What is the rationale for all comparisons returning false for IEEE754 NaN values? (1
我在哪里可以找到与 Python maketrans 和 translate 等效的 C# 代码?谢谢! 最佳答案 这应该带你到那里的大部分方式: public class MakeTrans {
我正在 SwiftUI 中构建一个应用程序其中包含很多组件,包括 Text包含长字符串的 View 。 当我在 iPhone 11 上启动该应用程序时,一切正常,但当我在较小的设备(例如 iPhone
这个问题已经有答案了: What is the equivalent lambda expression for System.out::println (2 个回答) Function pointe
我最近在使用 postgres,我必须做一些计算。然而我一直没能模仿Excel的HOUR()函数,我看了official information但这对我帮助不大。 函数接收一个小数,并得到小数的时、分
如果在 cython 中定义了一个指针 vector ,那么与 python 中的 enumerate 类似的函数或过程是什么,用于遍历指针数组中元素的索引和值在 C 声明类型函数内的循环中? 测试.
要选择: select user_id, max(case when value > 0 then timestamp else 0 end) as max_timestamp_whe
如果没有例子,这个问题很难问,所以这里是: #include struct O { }; struct C { template void function1(void (C::*call
我得到了这个结构的实现: struct NodoQ { Etype elem; NodoQ *sig; }; 下面是这段代码吗, typedef NodoQ *PtrNodoQ; PtrNod
我有一些宏需要访问当前类的类型,目前我通过违反 DRY 的模式解决了这个问题: struct ThisScruct{ int a; double b; //example st
我想知道 TensorFlow 的 softmax_cross_entropy_with_logits 是否有等效的 PyTorch 损失函数? 最佳答案 is there an equivalent
我找到了一个 trie 的 java 实现,并希望在 J2ME 中有一个类似的实现。这是代码。 节点类 import java.util.Collections; import java.util.L
我刚刚学习了 GraphQL,我想找到用户 id=2 OR 用户 id=3 现在我将如何进行 GraphQL 查询,我正在使用以下查询获取整个集合 { users() {
假设我有两个 Web 服务:A 和 B。两者都在 Apache 上运行。我希望它们可以从我的主机的不同端口访问:A 来自端口 88,B 来自端口 89。 我可以手动完成(首先创建图像,然后使用“doc
我一直在 excel 中使用一个非常简单的数组公式来处理一些数据集,但是它们变得太大并且在我更新计算时完全破坏了我的计算机性能。 excel表格和MySQL数据库布局如下: +-Timestamp-+
我有一个类,其实例要通过不同于它们携带的数据值的标识来区分。在我的代码中,我打算使用 == 来表示两个实例在它们的数据方面是等价的,并且 is 表示两个变量引用同一个实例,也就是说,他们是相同的。根据
我正在 Windows 中使用 WinSock 2.0 开发代理服务器。如果我想在阻塞模型中开发它,select() 是等待客户端或远程服务器从中接收数据的方法。是否有任何适用的方法可以使用 I/O
我正在将我制作的 Android 应用移植到 iOS。 Android 有一个 Yield() 函数可以将线程从运行中移到线程队列的后面(?)。这很有用,这样该线程就不会占用过多的 CPU 并使其他一
这是否保证始终为真: std::numeric_limits::max() == INT_MAX C++ 标准对此有何规定?我在标准中找不到任何明确说明这一点的引用资料,但我一直在阅读这些内容应该是等
我是一名优秀的程序员,十分优秀!