/
Stalnodg
/
RandomCoffee
Обзор
Документация
Войти
/
Stalnodg
/
RandomCoffee
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
app/src/test/java/com/topit/randomcoffee/presentation/viewmodel/MenuViewModelTest.kt
74 строки
2 KB
Max
presentation: pagination moved from data to presentation, bug with broken reload fixed, ViewModel for shopping cart created
05 май 2026, 18:21
05 май 2026, 18:21
c0f75ae
Код
Авторство
О чём код?
package com.topit.randomcoffee.presentation.viewmodel import com.topit.randomcoffee.domain.models.Category import com.topit.randomcoffee.domain.models.OperationResult import com.topit.randomcoffee.domain.repository.CoffeeRepository import com.topit.randomcoffee.domain.usecase.GetCategoriesUseCase import com.topit.randomcoffee.presentation.paging.GetPagedProducts import io.mockk.coEvery import io.mockk.mockk import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.advanceUntilIdle import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.setMain import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Before import org.junit.Test @OptIn(ExperimentalCoroutinesApi::class) class MenuViewModelTest { private val testDispatcher = StandardTestDispatcher() private lateinit var coffeeRepository: CoffeeRepository @Before fun setUp() { Dispatchers.setMain(testDispatcher) coffeeRepository = mockk(relaxed = true) } @After fun tearDown() { Dispatchers.resetMain() } @Test fun init_loads_categories() = runTest { val categories = listOf( Category(id = 1, name = "Coffee", slug = "coffee"), Category(id = 2, name = "Tea", slug = "tea") ) coEvery { coffeeRepository.getCategories() } returns OperationResult.Success(categories) val viewModel = createViewModel() advanceUntilIdle() assertEquals(categories, viewModel.uiState.value.categories) assertNull(viewModel.uiState.value.categoriesError) } @Test fun init_when_categories_fail_sets_error() = runTest { coEvery { coffeeRepository.getCategories() } returns OperationResult.Failure( com.topit.randomcoffee.domain.models.DomainError.Network() ) val viewModel = createViewModel() advanceUntilIdle() assertEquals("Не удалось загрузить категории", viewModel.uiState.value.categoriesError) } private fun createViewModel(): MenuViewModel { return MenuViewModel( getCategoriesUseCase = GetCategoriesUseCase(coffeeRepository), getPagedProducts = GetPagedProducts(coffeeRepository) ) } }