Skip to content

Commit

Permalink
Merge pull request #86 from ku-ring/2.0/kuring-101_test_yk
Browse files Browse the repository at this point in the history
kuring-101 FeedbackViewModel unit test 수정, MainDispatcherRule 추가
  • Loading branch information
yeon-kyu authored Jan 24, 2024
2 parents 8262d69 + 499a17e commit c5dcba3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import com.google.android.gms.tasks.OnSuccessListener
import com.google.android.gms.tasks.Task
import com.google.firebase.messaging.FirebaseMessaging
import com.ku_stacks.ku_ring.feedback.feedback.FeedbackViewModel
import com.ku_stacks.ku_ring.feedback.util.MainDispatcherRule
import com.ku_stacks.ku_ring.remote.util.DefaultResponse
import com.ku_stacks.ku_ring.thirdparty.firebase.analytics.EventAnalytics
import com.ku_stacks.ku_ring.user.repository.UserRepository
import com.ku_stacks.ku_ring.util.MockUtil
import com.ku_stacks.ku_ring.util.SchedulersTestRule
import io.reactivex.rxjava3.core.Single
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -41,6 +45,10 @@ class FeedbackViewModelTest {
@JvmField
val testSchedulersRule = SchedulersTestRule()

@OptIn(ExperimentalCoroutinesApi::class)
@get:Rule
val mainDispatcherRule = MainDispatcherRule()

@Before
fun setup() {
viewModel = FeedbackViewModel(userRepository, analytics, firebaseMessaging)
Expand Down Expand Up @@ -114,11 +122,13 @@ class FeedbackViewModelTest {
}
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `send Feedback Success Test`() {
fun `send Feedback Success Test`() = runTest {
// given
val mockFeedbackContent = "쿠링은 좋은 어플리케이션입니다."
viewModel.feedbackContent.value = mockFeedbackContent
viewModel.updateFeedbackContent(mockFeedbackContent)
viewModel.textStatus.first()

Mockito.`when`(firebaseMessaging.token).thenReturn(successTask)

Expand All @@ -138,11 +148,13 @@ class FeedbackViewModelTest {
assertEquals(R.string.feedback_success, viewModel.toastByResource.value)
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `send Feedback Fail Test - too short`() {
fun `send Feedback Fail Test - too short`() = runTest {
// given
val mockFeedbackContent = "짧은"
viewModel.feedbackContent.value = mockFeedbackContent
viewModel.updateFeedbackContent(mockFeedbackContent)
viewModel.textStatus.first()

Mockito.`when`(firebaseMessaging.token).thenReturn(successTask)

Expand All @@ -154,19 +166,21 @@ class FeedbackViewModelTest {
assertEquals(R.string.feedback_too_short, viewModel.toastByResource.value)
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `send Feedback Fail Test - too long`() {
fun `send Feedback Fail Test - too long`() = runTest {
// given
var mockFeedbackContent = "1"
repeat(256) {
mockFeedbackContent += "a"
}

viewModel.feedbackContent.value = mockFeedbackContent

Mockito.`when`(firebaseMessaging.token).thenReturn(successTask)

// when
viewModel.updateFeedbackContent(mockFeedbackContent)
viewModel.textStatus.first()

viewModel.sendFeedback()

// then
Expand All @@ -175,11 +189,13 @@ class FeedbackViewModelTest {
assertEquals(R.string.feedback_too_long, viewModel.toastByResource.value)
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `send Feedback Fail Test - server response error`() {
fun `send Feedback Fail Test - server response error`() = runTest {
// given
val mockFeedbackContent = "쿠링은 좋은 어플리케이션입니다."
viewModel.feedbackContent.value = mockFeedbackContent
viewModel.updateFeedbackContent(mockFeedbackContent)
viewModel.textStatus.first()

Mockito.`when`(firebaseMessaging.token).thenReturn(successTask)

Expand All @@ -199,4 +215,4 @@ class FeedbackViewModelTest {
verify(userRepository, times(1)).sendFeedback(mockFeedbackContent)
assertEquals(expectedResponseMsg, viewModel.toast.value)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ku_stacks.ku_ring.feedback.util

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description

@OptIn(ExperimentalCoroutinesApi::class)
class MainDispatcherRule(
private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher()
) : TestWatcher() {
override fun starting(description: Description) {
Dispatchers.setMain(testDispatcher)
}

override fun finished(description: Description) {
Dispatchers.resetMain()
}
}

0 comments on commit c5dcba3

Please sign in to comment.