Wednesday 11 September 2019

Test injected object with coroutines

My presenter has a injected empty object. When I run my use case (with coroutines) I would like check the contain of my object, but always is empty.

My use case just take the list objects and modify some param.

class CheckoutPresenter(
    private val view: CheckoutView?,
    private val getProductsFromCartUseCase: GetProductsFromCartUseCase,
    private val getTotalPriceUseCase: GetTotalPriceUseCase,
    private var products: MutableList<Product>
) {

 fun getTotalPrice() {
        getTotalPriceUseCase.setData(products)
        getTotalPriceUseCase.execute(this::onSuccessTotalPriceProducts, this::onFailProducts)
    }

private fun onSuccessTotalPriceProducts(products: MutableList<Product>) {
        this.products = products
        ...
}

My Test

@Test
fun ` example`() = testDispatcher.runBlockingTest {
    MainScope().launch {
        //Given
        products = ProductsMotherObject.createProductModel().values.toMutableList()
        presenter = CheckoutPresenter(view, getProductsFromCartUseCase, getTotalPriceUseCase, products)

        //when
        presenter.getTotalPrice()

        //then
        products //when I debug doesn't contain the information.
    }
}

My product object test is still empty



from Test injected object with coroutines

No comments:

Post a Comment