Android 如何模拟kotlin映射函数?
我正在尝试存根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
.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
classmapTo
函数:
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.