首页 > 解决方案 > 如何伪造 Result.toMappedResponse?

问题描述

尝试制作用于测试的假类不知道如何使其返回所需的带有 MyData 对象的列表。

 val result = Result.toMappedResponse(
    //Put something here to make it return List of MyData
)
override suspend fun getMyData(
    ex: String,
    us: String
): Result<List<MyData>> {
    return result
}

而且这不是伪造的原始功能

override suspend fun getMyData(
    ex: String,
    us: String
): Result<List<MyData>> {
    return Result.toMappedResponse(
        {
            toMyDataList(data, this)
        },
        { myApi.getDataList() }
    )
}

错误说

Not enough information to infer type variable T
public final inline fun <reified T, reified M> toMappedResponse(
mapper: M.() → T,
operation: () → Response<M?> ): Result<T>

并且描述 Helper 来包装操作并根据异常的缺席或存在返回 Success 或 Failure。如果正文为空,则返回 Empty。mapper 用于将操作转换为易于使用的数据。由于协程的性质,try/catch 里程会有所不同。

标签: androidkotlinandroid-espresso

解决方案


推荐阅读