add unit tests

This commit is contained in:
Pritimay Sarkar
2024-01-19 15:12:26 +05:30
parent 240840d74c
commit 7cf1d838ad
7 changed files with 175 additions and 209 deletions

View File

@@ -14,6 +14,7 @@ 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
@@ -23,57 +24,58 @@ import org.junit.Test
@ExperimentalCoroutinesApi
class AutoDacViewModelTest {
//
// @get:Rule
// val instantTaskExecutorRule = InstantTaskExecutorRule()
//
// private lateinit var viewModel: AutoDacViewModel
// private val hemoCubeDao = mockk<HemoCubeDao>()
// private val repository = mockk<Repository>()
// private val context = mockk<Context>()
// private val sharedPreferences = mockk<SharedPreferences>()
// private val networkStatusLiveData = mockk<NetworkStatusLiveData>()
//
// @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<DiagnosticsData>()
//
// // When
// viewModel.addAutoDacDataToDb(diagnosticsData)
//
// // Then
// assert(viewModel.fireBaseUpload.value == "Success")
// }
// @Test
// fun `test addAutoDacDataToDb error`() = runBlocking {
// // Given
// every { repository.addDiagnostics(any()) } returns Response.Error(Exception("Test error"))
//
// // When
// viewModel.addAutoDacDataToDb(mockk())
//
// // Then
// assert(viewModel.fireBaseUpload.value == "Error")
// }
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var viewModel: AutoDacViewModel
private val hemoCubeDao = mockk<HemoCubeDao>()
private val repository = mockk<Repository>()
private val context = mockk<Context>()
private val sharedPreferences = mockk<SharedPreferences>()
private val networkStatusLiveData = mockk<NetworkStatusLiveData>()
@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<DiagnosticsData>()
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")
}
}