Skip to content

Commit 3561772

Browse files
committed
refactor: advanceUntilIdle 함수 추가를 통한 코루틴 실행 보장
1 parent ac19953 commit 3561772

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

app/src/test/java/woowacourse/shopping/ui/cart/CartViewModelTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.mockk.coEvery
55
import io.mockk.mockk
66
import io.mockk.unmockkAll
77
import kotlinx.coroutines.ExperimentalCoroutinesApi
8+
import kotlinx.coroutines.test.advanceUntilIdle
89
import kotlinx.coroutines.test.runTest
910
import org.junit.jupiter.api.AfterEach
1011
import org.junit.jupiter.api.BeforeEach
@@ -74,6 +75,7 @@ class CartViewModelTest {
7475
setUpTestLiveData(CartProductUiModel(cartProducts = DUMMY_PRODUCTS_1), "_uiModel", viewModel)
7576

7677
viewModel.removeCartProduct(DUMMY_PRODUCT_1.cartId!!, DUMMY_PRODUCT_1.productDetail.id)
78+
advanceUntilIdle()
7779

7880
val state = viewModel.uiModel.getOrAwaitValue()
7981
assertThat(state.editedProductIds).contains(DUMMY_PRODUCT_1.productDetail.id)
@@ -87,6 +89,7 @@ class CartViewModelTest {
8789
setUpTestLiveData(CartProductUiModel(cartProducts = DUMMY_PRODUCTS_1), "_uiModel", viewModel)
8890

8991
viewModel.increaseCartProductQuantity(DUMMY_PRODUCT_1.productDetail.id)
92+
advanceUntilIdle()
9093

9194
val state = viewModel.uiModel.getOrAwaitValue()
9295
assertThat(state.cartProducts.getProductByProductId(DUMMY_PRODUCT_1.productDetail.id)?.quantity).isEqualTo(10)
@@ -100,6 +103,7 @@ class CartViewModelTest {
100103
setUpTestLiveData(CartProductUiModel(cartProducts = DUMMY_PRODUCTS_1), "_uiModel", viewModel)
101104

102105
viewModel.decreaseCartProductQuantity(DUMMY_PRODUCT_1.productDetail.id)
106+
advanceUntilIdle()
103107

104108
val state = viewModel.uiModel.getOrAwaitValue()
105109
assertThat(state.cartProducts.getProductByProductId(DUMMY_PRODUCT_1.productDetail.id)?.quantity).isEqualTo(3)
@@ -149,6 +153,7 @@ class CartViewModelTest {
149153
setUpTestLiveData(CartProductUiModel(recommendedProducts = DUMMY_PRODUCTS_1), "_uiModel", viewModel)
150154

151155
viewModel.increaseRecommendedProductQuantity(DUMMY_PRODUCT_1.productDetail.id)
156+
advanceUntilIdle()
152157

153158
val state = viewModel.uiModel.getOrAwaitValue()
154159
assertThat(state.recommendedProducts.getProductByProductId(updated.productDetail.id)?.quantity).isEqualTo(updatedQuantity)
@@ -165,6 +170,7 @@ class CartViewModelTest {
165170
setUpTestLiveData(CartProductUiModel(recommendedProducts = DUMMY_PRODUCTS_1), "_uiModel", viewModel)
166171

167172
viewModel.decreaseRecommendedProductQuantity(DUMMY_PRODUCT_1.productDetail.id)
173+
advanceUntilIdle()
168174

169175
val state = viewModel.uiModel.getOrAwaitValue()
170176
assertThat(state.recommendedProducts.getProductByProductId(updated.productDetail.id)?.quantity).isEqualTo(updatedQuantity)

app/src/test/java/woowacourse/shopping/ui/catalog/CatalogViewModelTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.mockk.mockk
88
import io.mockk.mockkStatic
99
import io.mockk.unmockkAll
1010
import kotlinx.coroutines.ExperimentalCoroutinesApi
11+
import kotlinx.coroutines.test.advanceUntilIdle
1112
import kotlinx.coroutines.test.runTest
1213
import org.junit.jupiter.api.AfterEach
1314
import org.junit.jupiter.api.BeforeEach
@@ -91,6 +92,7 @@ class CatalogViewModelTest {
9192
)
9293

9394
viewModel.loadMoreCatalogProducts()
95+
advanceUntilIdle()
9496

9597
val state = viewModel.uiModel.getOrAwaitValue()
9698
assertThat(state.catalogProducts.products).containsExactlyElementsIn(
@@ -113,6 +115,7 @@ class CatalogViewModelTest {
113115
)
114116

115117
viewModel.increaseCartProduct(DUMMY_PRODUCT_1.productDetail.id)
118+
advanceUntilIdle()
116119

117120
val result = viewModel.uiModel.getOrAwaitValue()
118121
assertThat(result.catalogProducts.getProductByProductId(updatedProduct.productDetail.id)?.quantity).isEqualTo(10)
@@ -133,6 +136,7 @@ class CatalogViewModelTest {
133136
)
134137

135138
viewModel.decreaseCartProduct(DUMMY_PRODUCT_1.productDetail.id)
139+
advanceUntilIdle()
136140

137141
val result = viewModel.uiModel.getOrAwaitValue()
138142
assertThat(result.catalogProducts.getProductByProductId(updatedProduct.productDetail.id)?.quantity).isEqualTo(3)
@@ -152,6 +156,7 @@ class CatalogViewModelTest {
152156
)
153157

154158
viewModel.loadCartProduct(DUMMY_PRODUCT_1.productDetail.id)
159+
advanceUntilIdle()
155160

156161
val result = viewModel.uiModel.getOrAwaitValue()
157162
assertThat(result.catalogProducts.getProductByProductId(updatedProduct.productDetail.id)?.quantity).isEqualTo(1234)
@@ -171,6 +176,7 @@ class CatalogViewModelTest {
171176
)
172177

173178
viewModel.loadCartProductsByProductIds(listOf(DUMMY_PRODUCT_1.productDetail.id))
179+
advanceUntilIdle()
174180

175181
val result = viewModel.uiModel.getOrAwaitValue()
176182
assertThat(result.catalogProducts.products).containsExactlyElementsIn(updated)
@@ -182,6 +188,7 @@ class CatalogViewModelTest {
182188
coEvery { getSearchHistoryUseCase() } returns Result.success(listOf(DUMMY_HISTORY_PRODUCT_1))
183189

184190
viewModel.loadHistoryProducts()
191+
advanceUntilIdle()
185192

186193
val result = viewModel.uiModel.getOrAwaitValue()
187194
assertThat(result.historyProducts).containsExactly(DUMMY_HISTORY_PRODUCT_1)
@@ -193,6 +200,7 @@ class CatalogViewModelTest {
193200
coEvery { getCartProductsQuantityUseCase() } returns Result.success(10)
194201

195202
viewModel.loadCartProductsQuantity()
203+
advanceUntilIdle()
196204

197205
val result = viewModel.uiModel.getOrAwaitValue()
198206
assertThat(result.cartProductsQuantity).isEqualTo(10)
@@ -205,6 +213,7 @@ class CatalogViewModelTest {
205213
coEvery { getCatalogProductsUseCase(any(), any()) } returns Result.failure(exception)
206214

207215
viewModel.loadMoreCatalogProducts()
216+
advanceUntilIdle()
208217

209218
val state = viewModel.uiModel.getOrAwaitValue()
210219
assertThat(state.connectionErrorMessage).contains("ERROR")

app/src/test/java/woowacourse/shopping/ui/payment/PaymentViewModelTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.mockk.mockk
88
import io.mockk.mockkStatic
99
import io.mockk.unmockkAll
1010
import kotlinx.coroutines.ExperimentalCoroutinesApi
11+
import kotlinx.coroutines.test.advanceUntilIdle
1112
import kotlinx.coroutines.test.runTest
1213
import org.junit.jupiter.api.AfterEach
1314
import org.junit.jupiter.api.BeforeEach
@@ -59,7 +60,10 @@ class PaymentViewModelTest {
5960
coEvery { getCouponsUseCase() } returns Result.success(DUMMY_COUPONS_1)
6061

6162
viewModel.loadProducts(DUMMY_PRODUCTS_3.products.map { it.productDetail.id })
63+
advanceUntilIdle()
64+
6265
viewModel.loadCoupons(DUMMY_PRODUCTS_3, DUMMY_LOCAL_DATE_TIME_1)
66+
advanceUntilIdle()
6367

6468
val state = viewModel.uiModel.getOrAwaitValue()
6569

@@ -77,7 +81,10 @@ class PaymentViewModelTest {
7781
coEvery { getCouponsUseCase() } returns Result.success(DUMMY_COUPONS_1)
7882

7983
viewModel.loadProducts(DUMMY_PRODUCTS_3.products.map { it.productDetail.id })
84+
advanceUntilIdle()
85+
8086
viewModel.loadCoupons(DUMMY_PRODUCTS_3, DUMMY_LOCAL_DATE_TIME_1)
87+
advanceUntilIdle()
8188

8289
viewModel.selectCoupon(
8390
DUMMY_COUPONS_1.value
@@ -100,8 +107,10 @@ class PaymentViewModelTest {
100107
coEvery { orderProductsUseCase(any()) } returns Result.success(Unit)
101108

102109
viewModel.loadProducts(DUMMY_PRODUCTS_1.products.map { it.productDetail.id })
110+
advanceUntilIdle()
103111

104112
viewModel.orderProducts()
113+
advanceUntilIdle()
105114

106115
val state = viewModel.uiModel.getOrAwaitValue()
107116
assertThat(state.isOrderSuccess).isTrue()
@@ -116,8 +125,10 @@ class PaymentViewModelTest {
116125
coEvery { orderProductsUseCase(any()) } returns Result.failure(exception)
117126

118127
viewModel.loadProducts(DUMMY_PRODUCTS_1.products.map { it.productDetail.id })
128+
advanceUntilIdle()
119129

120130
viewModel.orderProducts()
131+
advanceUntilIdle()
121132

122133
val state = viewModel.uiModel.getOrAwaitValue()
123134
assertThat(state.isOrderSuccess).isFalse()

app/src/test/java/woowacourse/shopping/ui/productdetail/ProductDetailViewModelTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.mockk.coEvery
55
import io.mockk.mockk
66
import io.mockk.unmockkAll
77
import kotlinx.coroutines.ExperimentalCoroutinesApi
8+
import kotlinx.coroutines.test.advanceUntilIdle
89
import kotlinx.coroutines.test.runTest
910
import org.junit.jupiter.api.AfterEach
1011
import org.junit.jupiter.api.BeforeEach
@@ -58,6 +59,7 @@ class ProductDetailViewModelTest {
5859

5960
// when
6061
viewModel.loadProductDetail(expected.productDetail.id)
62+
advanceUntilIdle()
6163

6264
// then
6365
val state = viewModel.uiModel.getOrAwaitValue()
@@ -74,6 +76,7 @@ class ProductDetailViewModelTest {
7476

7577
// when
7678
viewModel.loadProductDetail(999)
79+
advanceUntilIdle()
7780

7881
// then
7982
val state = viewModel.uiModel.getOrAwaitValue()
@@ -89,6 +92,7 @@ class ProductDetailViewModelTest {
8992

9093
// when
9194
viewModel.loadLastHistoryProduct()
95+
advanceUntilIdle()
9296

9397
// then
9498
val state = viewModel.uiModel.getOrAwaitValue()
@@ -136,6 +140,7 @@ class ProductDetailViewModelTest {
136140

137141
// when
138142
viewModel.updateCartProduct()
143+
advanceUntilIdle()
139144

140145
// then
141146
val state = viewModel.uiModel.getOrAwaitValue()

0 commit comments

Comments
 (0)