add unit tests
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user