I am trying to unit test my ViewModel. My code is written using Kotlin, Android-X and Jetpack compose and I am using Hilt for Dependency Injection and Mockk for... Mocking
The issue I'm having is that when I run the test testMainMenuViewModel
I receive the following error:
expected:<[image1.jpg, image2.jpg]> but was:<[]>
Expected :[image1.jpg, image2.jpg]
Actual :[]
that relates to this line:
assertEquals(galleryList, viewModel.photoGalleryState.value)
photoGalleryState.value is assigned in the MainMenuViewModel, i.e.
photoGalleryState.value = galleryRepository.getImageGalleryList()
and galleryRepository is mocked and 'every' call to getImageGalleryList
is handled.
I'm not clear if I'm doing something wrong, i.e. the way I'm evaluating, or if I need to do something different because I am using Hilt.
I am using Hilt 2.46.1 (which doesn't support HilViewModelTest) and Mockk 1.13.5
In addition, if I add in the line
verify { viewModel.photoGalleryState.value }
I receive the error
Missing calls inside verify { ... } block.
io.mockk.MockKException: Missing calls inside verify { ... } block.
My ViewModel class.
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import com.xgame.xgarage.model.MenuItem
import com.xgame.xgarage.model.MenuItemRepository
import com.xgame.xgarage.model.MenuPhotoGalleryRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
@HiltViewModel
class MainMenuViewModel @Inject constructor(
galleryRepository: MenuPhotoGalleryRepository,
menuItemRepository: MenuItemRepository
) : ViewModel() {
val photoGalleryState: MutableState<List<String>> = mutableStateOf(emptyList())
val itemMenuState: MutableState<List<MenuItem>> = mutableStateOf(emptyList())
init {
photoGalleryState.value = galleryRepository.getImageGalleryList()
itemMenuState.value = menuItemRepository.getMenuItemsList()
}
}
My ViewModel Unit Test
import com.xgames.xgarage.model.MenuItem
import com.xgames.xgarage.model.MenuItemRepository
import com.xgames.xgarage.model.MenuPhotoGalleryRepository
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.RelaxedMockK
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
class MainMenuViewModelTest {
@RelaxedMockK
private lateinit var galleryRepository: MenuPhotoGalleryRepository
@RelaxedMockK
private lateinit var menuItemRepository: MenuItemRepository
private lateinit var viewModel: MainMenuViewModel
@Before
fun setup() {
MockKAnnotations.init(this)
//galleryRepository = mockk<MenuPhotoGalleryRepository>()
//menuItemRepository = mockk<MenuItemRepository>()
viewModel = MainMenuViewModel(galleryRepository, menuItemRepository)
}
@Test
fun testMainMenuViewModel() {
// Prepare mock data
val galleryList = listOf("image1.jpg", "image2.jpg")
val menuList = listOf(MenuItem("file://some/file/name1.jpg", "1"), MenuItem("file://some/file/name2.jpg", "2"))
// Mock repository methods
every { galleryRepository.getImageGalleryList() } returns galleryList
every { menuItemRepository.getMenuItemsList() } returns menuList
// Verify the initial state of the view model
//viewModel.photoGalleryState.value
assertEquals(galleryList, viewModel.photoGalleryState.value)
//assertEquals(menuList, viewModel.itemMenuState.value)
}
}
MenuPhotoGalleryRepository
class MenuPhotoGalleryRepository {
private val imageGalleryX = listOf(
"file:///android_asset/images/cars/x1.jpg",
"file:///android_asset/images/cars/x2.jpg",
"file:///android_asset/images/cars/x3.jpg"
)
fun getImageGalleryList(): List<String> {
return imageGalleryX
}
}
MenuItem class
data class MenuItem(val imagePath: String, val menuName: String)
Usage of the ViewModel within my MainActivity class (used within a composable)
composable(route = "main_menu_screen") {
val viewModel: MainMenuViewModel = hiltViewModel()
MainMenuScreen(viewModel.photoGalleryState.value, viewModel.itemMenuState.value)
}
Any help or guidance is gratefully appreciated.
I think you need to change the test code like this
@Test
fun testMainMenuViewModel() {
// Prepare mock data
val galleryList = listOf("image1.jpg", "image2.jpg")
val menuList = listOf(MenuItem("file://some/file/name1.jpg", "1"), MenuItem("file://some/file/name2.jpg", "2"))
// Mock repository methods
every { galleryRepository.getImageGalleryList() } returns galleryList
every { menuItemRepository.getMenuItemsList() } returns menuList
// create view model after defining actions of mock
viewModel = MainMenuViewModel(galleryRepository, menuItemRepository)
// Verify the initial state of the view model
//viewModel.photoGalleryState.value
assertEquals(galleryList, viewModel.photoGalleryState.value)
//assertEquals(menuList, viewModel.itemMenuState.value)
}
When you create a view model in setup function, the init block of the view model will be invoked. So, the mocks are relaxed, they return empty list. I guess that what happened.