Files
hpos/app/src/test/java/com/example/hpostesting/AutoDacViewModelTest.kt

92 lines
3.4 KiB
Kotlin
Raw Normal View History

2024-03-18 13:16:11 +05:30
/*
* // Copyright (c) 2024 ShanMukha Innovations Pvt. Ltd. All rights reserved.
* // Notice: All information contained herein is, and remains
* // the property of ShanMukha Innovations Pvt. Ltd. and its suppliers,
* // if any. The intellectual and technical concepts contained
* // herein are proprietary to ShanMukha Innovations Pvt. Ltd.
* // and its suppliers and may be covered by Indian and Foreign Patents,
* // patents in process, and are protected by trade secret or copyright law.
* // Dissemination of this information or reproduction of this material
* // is strictly forbidden unless prior written permission is obtained
* // from ShanMukha Innovations Pvt. Ltd.
*/
2024-01-18 12:08:03 +05:30
package com.example.hpostesting
import android.content.Context
import android.content.SharedPreferences
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
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.repository.Repository
import com.example.hpostesting.presentation.autodac.AutoDacViewModel
2025-01-28 17:12:06 +05:30
import com.example.hpostesting.util.NetworkMonitor
2024-01-18 12:08:03 +05:30
import io.mockk.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
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 {
2024-01-19 15:12:26 +05:30
@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>()
2025-01-28 17:12:06 +05:30
private val networkStatusLiveData = mockk<NetworkMonitor>()
2024-01-19 15:12:26 +05:30
@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
2025-01-28 17:12:06 +05:30
every { networkStatusLiveData.startMonitoring() } just Runs
2024-01-19 15:12:26 +05:30
every { hemoCubeDao.getAll() } returns mockk()
2024-01-18 12:08:03 +05:30
// every { repository.addDiagnostics(any()) } returns Response.Success(Unit)
2024-01-19 15:12:26 +05:30
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")
}
2024-01-18 12:08:03 +05:30
}