Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kuring-101 FeedbackViewModel unit test 수정, MainDispatcherRule 추가 #86

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
Loading