Add RecipeViewModel tests
This commit is contained in:
@@ -122,6 +122,7 @@ dependencies {
|
||||
testImplementation(libs.robolectric)
|
||||
|
||||
testImplementation(libs.androidx.test.junit)
|
||||
testImplementation(libs.androidx.coreTesting)
|
||||
|
||||
testImplementation(libs.google.truth)
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package gq.kirmanak.mealient.ui.recipes
|
||||
|
||||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import gq.kirmanak.mealient.test.FakeLogger
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.every
|
||||
import io.mockk.impl.annotations.MockK
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class RecipeViewModelTest {
|
||||
|
||||
@MockK
|
||||
lateinit var authRepo: AuthRepo
|
||||
|
||||
@MockK(relaxed = true)
|
||||
lateinit var recipeRepo: RecipeRepo
|
||||
|
||||
private val logger: Logger = FakeLogger()
|
||||
|
||||
@get:Rule
|
||||
val instantExecutorRule = InstantTaskExecutorRule()
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
Dispatchers.setMain(UnconfinedTestDispatcher())
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
Dispatchers.resetMain()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when authRepo isAuthorized changes to true expect isAuthorized update`() {
|
||||
every { authRepo.isAuthorizedFlow } returns flowOf(false, true)
|
||||
assertThat(createSubject().isAuthorized.value).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when authRepo isAuthorized changes to false expect isAuthorized update`() {
|
||||
every { authRepo.isAuthorizedFlow } returns flowOf(true, false)
|
||||
assertThat(createSubject().isAuthorized.value).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when authRepo isAuthorized doesn't change expect isAuthorized null`() {
|
||||
every { authRepo.isAuthorizedFlow } returns flowOf(true)
|
||||
assertThat(createSubject().isAuthorized.value).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when isAuthorization change is handled expect isAuthorized null`() {
|
||||
every { authRepo.isAuthorizedFlow } returns flowOf(true, false)
|
||||
val subject = createSubject()
|
||||
subject.onAuthorizationChangeHandled()
|
||||
assertThat(subject.isAuthorized.value).isNull()
|
||||
}
|
||||
|
||||
private fun createSubject() = RecipeViewModel(recipeRepo, authRepo, logger)
|
||||
}
|
||||
@@ -35,6 +35,8 @@ swipeRefreshLayout = "1.1.0"
|
||||
splashScreen = "1.0.0"
|
||||
# https://developer.android.com/jetpack/androidx/releases/lifecycle
|
||||
lifecycle = "2.5.1"
|
||||
# https://developer.android.com/jetpack/androidx/releases/arch-core
|
||||
coreTesting = "2.1.0"
|
||||
# https://github.com/square/retrofit/tags
|
||||
retrofit = "2.9.0"
|
||||
# https://github.com/JakeWharton/retrofit2-kotlinx-serialization-converter/tags
|
||||
@@ -121,6 +123,7 @@ androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version
|
||||
androidx-constraintLayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "contraintLayout" }
|
||||
androidx-swipeRefreshLayout = { group = "androidx.swiperefreshlayout", name = "swiperefreshlayout", version.ref = "swipeRefreshLayout" }
|
||||
androidx-splashScreen = { group = "androidx.core", name = "core-splashscreen", version.ref = "splashScreen" }
|
||||
androidx-coreTesting = { group = "androidx.arch.core", name = "core-testing", version.ref = "coreTesting" }
|
||||
|
||||
androidx-paging-runtimeKtx = { group = "androidx.paging", name = "paging-runtime-ktx", version.ref = "paging" }
|
||||
androidx-paging-commonKtx = { group = "androidx.paging", name = "paging-common-ktx", version.ref = "paging" }
|
||||
|
||||
Reference in New Issue
Block a user