UnitTest协程Kotlin用例MVP

时间:2019-07-03 21:03:31

标签: android unit-testing kotlin-coroutines

我正在尝试从用例中模拟响应,该用例适用于协同程序。

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

我的useCase注入了演示者。

GetProductsUseCase具有以下代码:

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

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

我的BaseUseCase

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)
                }
            }
        }
    }

}

此useCase调用我的存储库:

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();"

1 个答案:

答案 0 :(得分:4)

问题在于您如何创建@post('/resource-groups', { responses: { '200': { description: 'ResourceGroup model instance', content: {'application/json': {schema: {'x-ts-type': ResourceGroup}}}, }, }, }) async create(@requestBody() resourceGroup: ResourceGroup): Promise<ResourceGroup> { return await this.resourceGroupRepository.create(resourceGroup); }

您不是使用GetProductsUseCase的{​​{1}}版本创建它,而是嘲笑mocked调用。

尝试手动创建ProductsRepository,而不使用ProductsRepository

GetProductsUseCase