Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何模拟kotlin映射函数?_Android_Kotlin_Mocking - Fatal编程技术网

Android 如何模拟kotlin映射函数?

Android 如何模拟kotlin映射函数?,android,kotlin,mocking,Android,Kotlin,Mocking,我正在尝试存根kotlin扩展名.map: private fun stubGetUsers(users: List<User>) { val genericRawResult = mock<GenericRawResults<User>>() whenever(genericRawResult.map { User() }).thenReturn(users) usersDao.stub { on { it.queryRaw("", Ra

我正在尝试存根kotlin扩展名
.map

private fun stubGetUsers(users: List<User>) {
   val genericRawResult = mock<GenericRawResults<User>>()
   whenever(genericRawResult.map { User() }).thenReturn(users)

   usersDao.stub { on { it.queryRaw("", RawRowMapper { _, _ -> User() }) }.doReturn(genericRawResult) }
}
第行:
用于(本项目)

因此,我尝试模拟
\u Collections.kt
class
mapTo
函数:

val destination = arrayListOf<User>()

whenever<List<User>>(genericRawResult.mapTo(destination) { User() }).thenReturn(destination)
为了获得详细错误,我做了以下更改:

genericRawResult.stub { onGeneric { it.mapTo(arrayListOf()) { User() } }.doReturn(arrayListOf()) }
genericRawResult.stub { on { it.map { User() } }.doReturn(users) }
这就是我得到的错误:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
ArrayList cannot be returned by iterator()
iterator() should return Iterator
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

GenericRawResults
是接口还是扩展了集合类型?您可能应该将其建模为一个接口,并拥有一个委托给集合的实现,在这种情况下,模拟
GenericRawResults.map
将非常简单。是的,它是一个来自ORM库的接口:此接口实现另一个Java接口:
Iterable
,这就是为什么我可以访问Kotlin的
Collection
map函数。看看是否可以将依赖于
GenericRawResults
的代码更改为依赖于
Iterable
,相反,你可以更轻松地模拟
Iterable
GenericRawResults
是一个接口,还是它扩展了一个集合类型?您可能应该将其建模为一个接口,并拥有一个委托给集合的实现,在这种情况下,模拟
GenericRawResults.map
将非常简单。是的,它是一个来自ORM库的接口:此接口实现另一个Java接口:
Iterable
,这就是为什么我可以访问Kotlin的
Collection
map函数。看看是否可以将依赖于
GenericRawResults
的代码更改为依赖于
Iterable
,相反,你将更容易模拟
Iterable
genericRawResult.stub { onGeneric { it.mapTo(arrayListOf()) { User() } }.doReturn(arrayListOf()) }
genericRawResult.stub { on { it.map { User() } }.doReturn(users) }
org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
ArrayList cannot be returned by iterator()
iterator() should return Iterator
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.