refactoring hemocube fragment and add tests

This commit is contained in:
Pritimay Sarkar
2024-01-23 13:42:27 +05:30
parent a0fc7f7be4
commit f83823a15f
6 changed files with 210 additions and 75 deletions

View File

@@ -1,27 +1,20 @@
package com.example.hpostesting
import android.content.Context
import android.os.Build
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.Observer
import androidx.work.Configuration
import androidx.work.testing.SynchronousExecutor
import androidx.work.testing.WorkManagerTestInitHelper
import com.example.hpostesting.data.Result
import com.example.hpostesting.data.model.login.LoginResponse
import com.example.hpostesting.data.repository.Repository
import com.example.hpostesting.domain.LogFileManager
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
import com.example.hpostesting.util.TestCoroutineRule
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.verify
import junit.framework.TestCase
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.After
import org.junit.Before
import org.junit.Rule
@@ -29,35 +22,34 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import java.io.File
//@RunWith(RobolectricTestRunner::class)
//@Config(manifest = Config.NONE)
//@ExperimentalCoroutinesApi
//class HemocubeViewModelTest {
//
// @get:Rule
// val instantTaskExecutorRule = InstantTaskExecutorRule()
//
// @get:Rule
// val coroutineRule = TestCoroutineRule()
//
// @MockK(relaxed = true)
// lateinit var repository: Repository
//
// @MockK(relaxed = true)
// lateinit var logFileManager: LogFileManager
//
// @MockK(relaxed = true)
// lateinit var context: Context
//
// @MockK(relaxed = true)
// lateinit var observer: Observer<Result<LoginResponse>>
//
// private lateinit var viewModel: HemoCubeViewModel
//
// @Before
// fun setup() {
@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE)
@ExperimentalCoroutinesApi
class HemocubeViewModelTest {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
@get:Rule
val coroutineRule = TestCoroutineRule()
@MockK(relaxed = true)
lateinit var repository: Repository
@MockK(relaxed = true)
lateinit var logFileManager: LogFileManager
@MockK(relaxed = true)
lateinit var context: Context
@MockK(relaxed = true)
lateinit var observer: Observer<Result<LoginResponse>>
private lateinit var viewModel: HemoCubeViewModel
@Before
fun setup() {
// MockKAnnotations.init(this)
//
// val config = Configuration.Builder()
@@ -78,13 +70,34 @@ import java.io.File
// context
// )
// viewModel.loginResponse.observeForever(observer)
// }
//
// @After
// fun teardown() {
}
@After
fun teardown() {
// viewModel.loginResponse.removeObserver(observer)
// }
//
}
@Test
fun `testAddNumbers in HemocubeViewModelTest`() {
// Create a mock of the Calculator class using Mockito-Kotlin
val calculatorMock = mockk<Calculator>()
// Define the behavior of the add method on the mock
every { calculatorMock.add(2, 3) } returns 5
// Create an instance of MathApplication with the mock Calculator
val mathApplication = MathApplication(calculatorMock)
// Perform the test using the MathApplication
val result = mathApplication.addNumbers(2, 3)
// Verify that the add method of the mock was called with the correct parameters
verify { calculatorMock.add(2, 3) }
// Verify the result of the test
TestCase.assertEquals(5, result)
}
// @Test
// fun `login() should update LiveData with success`() = coroutineRule.runBlockingTest {
//
@@ -124,7 +137,7 @@ import java.io.File
// viewModel.uploadLogs()
//
// // Assert
// verify { observer.onChanged(response) }
// verify { observer.onChanged(response as Result<LoginResponse>) }
// }
//
// @Test
@@ -141,4 +154,4 @@ import java.io.File
// // Assert
// verify { observer.onChanged(response) }
// }
//}
}