Skip to content

Commit

Permalink
[AN/USER] refactor: ViewModel Test Code Dispatcher Rule 추가 (#590)
Browse files Browse the repository at this point in the history
* feat: coroutine dispatcher main rule 추가

* refactor: 티켓 예매 화면 ViewModel 테스트 리팩터링

* refactor: 티켓 목록 화면 ViewModel 테스트 리팩터링

* refactor: 티켓 기록 화면 ViewModel 테스트 리팩터링

* refactor: 티켓 입장 화면 ViewModel 테스트 리팩터링

* refactor: 학생 인증 화면 ViewModel 테스트 리팩터링

* refactor: 로그인 화면 ViewModel 테스트 리팩터링

* refactor: 학교 선택 화면 ViewModel 테스트 리팩터링

* refactor: 마이페이지 화면 ViewModel 테스트 리팩터링

* refactor: 홈 화면 ViewModel 테스트 리팩터링

* refactor: 축제 목록 화면 ViewModel 테스트 리팩터링

* refactor: 마이페이지 화면 ViewModel 테스트 재변경
  • Loading branch information
SeongHoonC authored Oct 24, 2023
1 parent f781fe5 commit 8550e89
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 144 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.festago.festago.presentation.rule

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

// Reusable JUnit4 TestRule to override the Main dispatcher
class MainDispatcherRule
@OptIn(ExperimentalCoroutinesApi::class)
constructor(
private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(),
) : TestWatcher() {

@OptIn(ExperimentalCoroutinesApi::class)
override fun starting(description: Description) {
Dispatchers.setMain(testDispatcher)
}

@OptIn(ExperimentalCoroutinesApi::class)
override fun finished(description: Description) {
Dispatchers.resetMain()
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
package com.festago.festago.presentation.ui.home

import app.cash.turbine.test
import com.festago.festago.presentation.rule.MainDispatcherRule
import com.festago.festago.repository.AuthRepository
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class HomeViewModelTest {

private lateinit var vm: HomeViewModel
private lateinit var authRepository: AuthRepository

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

@Before
fun setUp() {
Dispatchers.setMain(UnconfinedTestDispatcher())
authRepository = mockk()
vm = HomeViewModel(authRepository)
}

@OptIn(ExperimentalCoroutinesApi::class)
@After
fun finish() {
Dispatchers.resetMain()
}

private fun `사용자 인증 유무가 다음과 같을 때`(isSigned: Boolean) {
every { authRepository.isSigned } returns isSigned
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@ package com.festago.festago.presentation.ui.home.festivallist
import app.cash.turbine.test
import com.festago.festago.analytics.AnalyticsHelper
import com.festago.festago.model.Festival
import com.festago.festago.presentation.rule.MainDispatcherRule
import com.festago.festago.repository.FestivalRepository
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.SoftAssertions
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.time.LocalDate

Expand All @@ -36,21 +32,16 @@ class FestivalListViewModelTest {
)
}

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

@Before
fun setUp() {
Dispatchers.setMain(UnconfinedTestDispatcher())
festivalRepository = mockk()
analyticsHelper = mockk(relaxed = true)
vm = FestivalListViewModel(festivalRepository, analyticsHelper)
}

@OptIn(ExperimentalCoroutinesApi::class)
@After
fun finish() {
Dispatchers.resetMain()
}

private fun `축제 목록 요청 결과가 다음과 같을 때`(result: Result<List<Festival>>) {
coEvery {
festivalRepository.loadFestivals()
Expand Down Expand Up @@ -131,7 +122,6 @@ class FestivalListViewModelTest {

@Test
fun `티켓 예매를 열면 티켓 예매 열기 이벤트가 발생한다`() = runTest {

vm.event.test {
// when
val fakeFestivalId = 1L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,23 @@ import com.festago.festago.model.Stage
import com.festago.festago.model.Ticket
import com.festago.festago.model.TicketCondition
import com.festago.festago.model.UserProfile
import com.festago.festago.presentation.rule.MainDispatcherRule
import com.festago.festago.repository.AuthRepository
import com.festago.festago.repository.TicketRepository
import com.festago.festago.repository.UserRepository
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.SoftAssertions
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.time.LocalDateTime

class MyPageViewModelTest {

private lateinit var vm: MyPageViewModel
private lateinit var userRepository: UserRepository
private lateinit var ticketRepository: TicketRepository
Expand Down Expand Up @@ -55,23 +52,18 @@ class MyPageViewModelTest {
),
)

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

@Before
fun setUp() {
Dispatchers.setMain(UnconfinedTestDispatcher())
userRepository = mockk(relaxed = true)
ticketRepository = mockk()
authRepository = mockk()
analyticsHelper = mockk(relaxed = true)
vm = MyPageViewModel(userRepository, ticketRepository, authRepository, analyticsHelper)
}

@OptIn(ExperimentalCoroutinesApi::class)
@After
fun finish() {
Dispatchers.resetMain()
}

private fun `로그인 상태가 다음과 같다`(result: Boolean) {
coEvery {
authRepository.isSigned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,33 @@ import app.cash.turbine.test
import com.festago.festago.analytics.AnalyticsHelper
import com.festago.festago.model.Ticket
import com.festago.festago.presentation.fixture.TicketFixture
import com.festago.festago.presentation.rule.MainDispatcherRule
import com.festago.festago.repository.TicketRepository
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.assertj.core.api.SoftAssertions
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class TicketListViewModelTest {

private lateinit var vm: TicketListViewModel
private lateinit var ticketRepository: TicketRepository
private lateinit var analyticsHelper: AnalyticsHelper

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

@Before
fun setUp() {
Dispatchers.setMain(UnconfinedTestDispatcher())
ticketRepository = mockk()
analyticsHelper = mockk(relaxed = true)
vm = TicketListViewModel(ticketRepository, analyticsHelper)
}

@OptIn(ExperimentalCoroutinesApi::class)
@After
fun finish() {
Dispatchers.resetMain()
}

private fun `현재 티켓 요청 결과가 다음과 같을 때`(result: Result<List<Ticket>>) {
coEvery { ticketRepository.loadCurrentTickets() } returns result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,28 @@ package com.festago.festago.presentation.ui.selectschool
import app.cash.turbine.test
import com.festago.festago.analytics.AnalyticsHelper
import com.festago.festago.model.School
import com.festago.festago.presentation.rule.MainDispatcherRule
import com.festago.festago.repository.SchoolRepository
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.SoftAssertions.assertSoftly
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class SelectSchoolViewModelTest {

private val testDispatcher = UnconfinedTestDispatcher()
private lateinit var vm: SelectSchoolViewModel
private lateinit var schoolRepository: SchoolRepository
private lateinit var analyticsHelper: AnalyticsHelper

@get:Rule
val mainDispatcherRule = MainDispatcherRule()

@Before
fun setup() {
Dispatchers.setMain(testDispatcher)
schoolRepository = mockk()
analyticsHelper = mockk(relaxed = true)
vm = SelectSchoolViewModel(
Expand All @@ -37,11 +33,6 @@ class SelectSchoolViewModelTest {
)
}

@After
fun finish() {
Dispatchers.resetMain()
}

private fun `학교 목록 불러오기 요청 결과가 다음과 같을 때 `(result: Result<List<School>>) {
coEvery {
schoolRepository.loadSchools()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,32 @@ package com.festago.festago.presentation.ui.signin

import app.cash.turbine.test
import com.festago.festago.analytics.AnalyticsHelper
import com.festago.festago.presentation.rule.MainDispatcherRule
import com.festago.festago.repository.AuthRepository
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class SignInViewModelTest {

private lateinit var vm: SignInViewModel
private lateinit var authRepository: AuthRepository
private lateinit var analyticsHelper: AnalyticsHelper

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

@Before
fun setUp() {
Dispatchers.setMain(UnconfinedTestDispatcher())

authRepository = mockk(relaxed = true)
analyticsHelper = mockk(relaxed = true)
vm = SignInViewModel(authRepository, analyticsHelper)
}

@OptIn(ExperimentalCoroutinesApi::class)
@After
fun finish() {
Dispatchers.resetMain()
}

private fun `로그인 결과가 다음과 같을 때`(result: Result<Unit>) {
coEvery { authRepository.signIn() } returns result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,30 @@ package com.festago.festago.presentation.ui.studentverification
import app.cash.turbine.test
import com.festago.festago.analytics.AnalyticsHelper
import com.festago.festago.model.StudentVerificationCode
import com.festago.festago.presentation.rule.MainDispatcherRule
import com.festago.festago.repository.SchoolRepository
import com.festago.festago.repository.StudentVerificationRepository
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.SoftAssertions.assertSoftly
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class StudentVerificationViewModelTest {

private val testDispatcher = UnconfinedTestDispatcher()
private lateinit var vm: StudentVerificationViewModel
private lateinit var studentVerificationRepository: StudentVerificationRepository
private lateinit var schoolRepository: SchoolRepository
private lateinit var analyticsHelper: AnalyticsHelper

@get:Rule
val mainDispatcherRule = MainDispatcherRule()

@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
studentVerificationRepository = mockk()
schoolRepository = mockk()
analyticsHelper = mockk(relaxed = true)
Expand All @@ -41,12 +37,6 @@ class StudentVerificationViewModelTest {
)
}

@OptIn(ExperimentalCoroutinesApi::class)
@After
fun finish() {
Dispatchers.resetMain()
}

private fun `이메일 요청 결과가 다음과 같을 때`(result: Result<String>) {
coEvery {
schoolRepository.loadSchoolEmail(any())
Expand Down
Loading

0 comments on commit 8550e89

Please sign in to comment.