gpt4 book ai didi

android - UnitTest 协程 Kotlin 用例 MVP

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:16 24 4
gpt4 key购买 nike

我正在尝试模拟来 self 的用例的响应,这个用例适用于协程。

fun getData() {
view?.showLoading()
getProductsUseCase.execute(this::onSuccessApi, this::onErrorApi)
}

我的用例是在演示者上注入(inject)的。

GetProductsUseCase 有这段代码:

class GetProductsUseCase (private var productsRepository: ProductsRepository) : UseCase<MutableMap<String, Product>>() {

override suspend fun executeUseCase(): MutableMap<String, Product> {
val products =productsRepository.getProductsFromApi()
return products
}
}

我的基本用例

abstract class UseCase<T> {

abstract suspend fun executeUseCase(): Any

fun execute(
onSuccess: (T) -> Unit,
genericError: () -> Unit) {
GlobalScope.launch {
val result = async {
try {
executeUseCase()
} catch (e: Exception) {
GenericError()
}
}
GlobalScope.launch(Dispatchers.Main) {
when {
result.await() is GenericError -> genericError()
else -> onSuccess(result.await() as T)
}
}
}
}

}

这个用例调用我的存储库:

override suspend fun getProductsFromApi(): MutableMap<String, Product> {
val productsResponse = safeApiCall(
call = {apiService.getProductsList()},
error = "Error fetching products"
)
productsResponse?.let {
return productsMapper.fromResponseToDomain(it)!!
}
return mutableMapOf()
}

Y 尝试模拟我的响应,但测试总是失败。

@RunWith(MockitoJUnitRunner::class)
class HomePresenterTest {

lateinit var presenter: HomePresenter

@Mock
lateinit var view: HomeView

@Mock
lateinit var getProductsUseCase: GetProductsUseCase

@Mock
lateinit var updateProductsUseCase: UpdateProductsUseCase

private lateinit var products: MutableMap<String, Product>

private val testDispatcher = TestCoroutineDispatcher()
private val testScope = TestCoroutineScope(testDispatcher)

@Mock
lateinit var productsRepository:ProductsRepositoryImpl

@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
products = ProductsMotherObject.createEmptyModel()
presenter = HomePresenter(view, getProductsUseCase, updateProductsUseCase, products)
}

@After
fun after() {
Dispatchers.resetMain()
testScope.cleanupTestCoroutines()
}

//...

@Test
fun a() = testScope.runBlockingTest {
setTasksNotAvailable(productsRepository)
presenter.getDataFromApi()

verify(view).setUpRecyclerView(products.values.toMutableList())
}

private suspend fun setTasksNotAvailable(dataSource: ProductsRepository) {
`when`(dataSource.getProductsFromApi()).thenReturn((mutableMapOf()))
}
}

我不知道发生了什么。日志说:

"Wanted but not invoked:
view.setUpRecyclerView([]);
-> at com.myProject.HomePresenterTest$a$1.invokeSuspend(HomePresenterTest.kt:165)

However, there was exactly 1 interaction with this mock:
view.showLoading();"

最佳答案

问题在于您如何创建 GetProductsUseCase

您不是使用ProductsRepositorymocked 版本创建它,而是在模拟ProductsRepository 调用。

尝试手动创建 GetProductsUseCase 而不是使用 @Mock

// no @Mock
lateinit var getProductsUseCase: GetProductsUseCase


@Before
fun setUp() {
// ...
// after your mocks are initialized...
getProductsUseCase = GetProductsUseCase(productsRepository) //<- this uses mocked ProductsRepository
}

关于android - UnitTest 协程 Kotlin 用例 MVP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56877889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com