package com.example.hpostesting import android.content.Context import android.content.SharedPreferences import androidx.arch.core.executor.testing.InstantTaskExecutorRule import androidx.lifecycle.Observer import com.example.hpostesting.data.NetworkStatusLiveData import com.example.hpostesting.data.dao.HemoCubeDao import com.example.hpostesting.data.model.Response import com.example.hpostesting.data.model.diagnostics.DiagnosticsData import com.example.hpostesting.data.model.patient.DeviceData import com.example.hpostesting.data.repository.Repository import com.example.hpostesting.presentation.autodac.AutoDacViewModel import io.mockk.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.TestCoroutineDispatcher import kotlinx.coroutines.test.setMain import org.junit.Before import org.junit.Rule import org.junit.Test @ExperimentalCoroutinesApi class AutoDacViewModelTest { @get:Rule val instantTaskExecutorRule = InstantTaskExecutorRule() private lateinit var viewModel: AutoDacViewModel private val hemoCubeDao = mockk() private val repository = mockk() private val context = mockk() private val sharedPreferences = mockk() private val networkStatusLiveData = mockk() @Before fun setup() { MockKAnnotations.init(this) // Mocking the Context and its dependencies every { context.applicationContext } returns context every { context.getSharedPreferences(any(), any()) } returns sharedPreferences every { context.getSystemService(any()) } returns networkStatusLiveData // Mocking the NetworkStatusLiveData every { networkStatusLiveData.observe(any(), any()) } just Runs every { hemoCubeDao.getAll() } returns mockk() // every { repository.addDiagnostics(any()) } returns Response.Success(Unit) viewModel = AutoDacViewModel(hemoCubeDao, repository, context) Dispatchers.setMain(TestCoroutineDispatcher()) } @Test fun `test addAutoDacDataToDb success`() = runBlocking { // Given val diagnosticsData = mockk() coEvery { repository.addDiagnostics(diagnosticsData) } returns Response.Success("Success") // When viewModel.addAutoDacDataToDb(diagnosticsData) // Then assert(viewModel.fireBaseUpload.value == "Success") } @Test fun `test addAutoDacDataToDb error`() = runBlocking { // Given coEvery { repository.addDiagnostics(any()) } returns Response.Error(Exception("Test error")) // When viewModel.addAutoDacDataToDb(mockk()) // Then assert(viewModel.fireBaseUpload.value == "Error") } }