【发布时间】:2020-08-30 03:40:38
【问题描述】:
我正在尝试测试方法getSongsList
class SongsRemoteDataSource @Inject constructor(
private val resultParser: ResultParser,
private val songsService: SongsService
) {
suspend fun getSongsList(query: String): Result<SongsResponse> =
resultParser.parse { songsService.getSongsList(query) }
}
基本上,我正在尝试测试是否使用正确的 lambda 作为参数调用了模拟
class SongsRemoteDataSourceTest {
@RelaxedMockK
private lateinit var resultParser: ResultParser
@RelaxedMockK
private lateinit var songsService: SongsService
private lateinit var songsRemoteDataSource: SongsRemoteDataSource
@Before
fun setUp() {
MockKAnnotations.init(this)
songsRemoteDataSource = SongsRemoteDataSource(resultParser, songsService)
}
@Test
fun getSongsList() = runBlockingTest {
val query = "query"
songsRemoteDataSource.getSongsList(query)
coVerify { resultParser.parse { songsService.getSongsList(query) } }
}
}
但测试失败
java.lang.AssertionError: Verification failed: call 1 of 1: ResultParser(resultParser#1).parse(eq(continuation {}), any())). Only one matching call to ResultParser(resultParser#1)/parse(Function1, Continuation) happened, but arguments are not matching:
[0]: argument: continuation {}, matcher: eq(continuation {}), result: -
[1]: argument: continuation {}, matcher: any(), result: +
结果解析器
class ResultParser @Inject constructor() {
suspend fun <T> parse(call: suspend () -> Response<T>): Result<T> {
...
}
}
歌曲服务
interface SongsService {
@GET("search")
suspend fun getSongsList(
@Query("term") query: String,
@Query("mediaType") mediaType: String = "music"
): Response<SongsResponse>
}
我不明白为什么它会失败。我做错了什么?
【问题讨论】: