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

BookmarkViewModelTest #39

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package kr.co.bookmark

import app.cash.turbine.test
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import kr.co.data.repository.BookmarkRepository
import kr.co.data.repository.RecentRepository
import kr.co.model.BookmarkSideEffect
import kr.co.model.BookmarkUiIntent
import kr.co.model.FileInfo
import kr.co.model.FileInfo.Type.PDF
import kr.co.testing.rule.CoroutineTestRule
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.time.LocalDateTime
import kotlin.test.assertEquals

internal class BookmarkViewModelTest {

@get: Rule
val coroutineTestRule = CoroutineTestRule()

@MockK
private lateinit var bookmarkRepository: BookmarkRepository

@MockK
private lateinit var recentRepository: RecentRepository

private lateinit var viewModel: BookmarkViewModel

@Before
fun setup() {
MockKAnnotations.init(this)
viewModel = BookmarkViewModel(bookmarkRepository,recentRepository)
}

@Test
fun `Given a Unit when Init intent is handled then state is updated`() = runTest {
val dummy = listOf(PDF_DUMMY, PDF_DUMMY)

coEvery { bookmarkRepository.get() } returns flowOf(dummy)

viewModel.handleIntent(BookmarkUiIntent.Init)

viewModel.uiState.test {
val state = awaitItem()
assertEquals(state.files, dummy)
}
}

@Test
fun `Given a file when ClickFile intent is handled then navigate to pdf`() = runTest {
val file = PDF_DUMMY

viewModel.handleIntent(BookmarkUiIntent.ClickFile(file))

viewModel.sideEffect.test {
awaitItem().also {
assert(it is BookmarkSideEffect.NavigateToPdf)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ•„λž˜μ™€ 같이 μž‘μ„±ν•˜λ©΄ 슀마트 μΊμŠ€νŒ…μ„ ν™œμš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

val effect = awaitItem()
assert(effect is BookmarkSideEffect.NavigateToPdf)
assert(effect.path == file.path)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requireλ₯Ό ν™œμš©ν•œ 방법은 μ–΄λ–€κ°€μš”

require(this is BookmarkSideEffect.NavigateToPdf)
assert(path == file.path)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν…ŒμŠ€νŠΈ μ½”λ“œμ—μ„œ requireλŠ” μ ν•©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
require의 μš©λ„λŠ” μž…λ ₯값에 λŒ€ν•œ 사전 쑰건 확인을 μ˜λ―Έν•©λ‹ˆλ‹€. 반면 assertλŠ” μ‹€ν–‰ κ²°κ³Όκ°€ κΈ°λŒ€ν•œ 바와 λ§žλŠ”μ§€μ— λŒ€ν•œ κ²€μ¦μž…λ‹ˆλ‹€.
ν…ŒμŠ€νŠΈ μ½”λ“œλŠ” λ™μž‘ν•œ μ½”λ“œμ˜ 결과물에 λŒ€ν•œ "검증" 이 λͺ©μ μ΄λ―€λ‘œ assertκ°€ μ‚¬μš©μ‚¬λ‘€μ— λ§žμŠ΅λ‹ˆλ‹€.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ „ assert둜 μ™œ 슀마트 μΊμŠ€νŒ…μ΄ μ•ˆλ˜μ£ ,,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음? 그럼 ν˜Ήμ‹œ assertTrue(effect is BookmarkSideEffect.NavigateToPdf) 둜 ν•΄λ³΄μ‹œκ² μ–΄μš”?
κ·Έλž˜λ„ μΊμŠ€νŒ…μ΄ μ•ˆλœλ‹€λ©΄ requireλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šμ€ 기쑴의 ν˜•νƒœλ‘œ ν•˜μ…”λ„ λ©λ‹ˆλ‹€.

assert((it as BookmarkSideEffect.NavigateToPdf).path == file.path)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertEqualsλ₯Ό μ‚¬μš©ν•˜λ©΄ 의미λ₯Ό 더 μ •ν™•ν•˜κ²Œ ν‘œν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
κ°’ 검증을 μœ„ν•œ λ‹€μ–‘ν•œ assertκ°€ μžˆμœΌλ‹ˆ ν™œμš©ν•΄λ³΄λ©΄ μ’‹κ² μŠ΅λ‹ˆλ‹€.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ‹€μŒ PRμ—μ„œ μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹€

}
}
}

companion object {
val PDF_DUMMY = FileInfo(
name = "DUMMY.pdf",
path = "",
type = PDF,
isDirectory = false,
isHidden = false,
size = 0,
createdAt = LocalDateTime.now(),
lastModified = LocalDateTime.now()
)

val FOLDER_DUMMY = FileInfo(
name = "DUMMY",
path = "",
type = PDF,
isDirectory = true,
isHidden = false,
size = 0,
createdAt = LocalDateTime.now(),
lastModified = LocalDateTime.now()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.test.runTest
import kr.co.data.repository.RecentRepository
import kr.co.model.ExploreSideEffect
import kr.co.model.ExploreUiIntent
import kr.co.model.FileInfo
import kr.co.model.FileInfo.Type.PDF
import kr.co.testing.repository.TestRecentRepository
import kr.co.testing.rule.CoroutineTestRule
import kr.co.util.FileManager
import org.junit.Before
Expand All @@ -26,7 +26,8 @@ class ExploreViewModelTest {

private lateinit var viewModel: ExploreViewModel

private val recentRepository = TestRecentRepository()
@MockK
private lateinit var recentRepository: RecentRepository

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„±μ— κ΄€ν•˜μ—¬ κ³ μ „νŒŒμ™€ 런던파 두 κ°€μ§€μ˜ ν˜•νƒœκ°€ μžˆμŠ΅λ‹ˆλ‹€.
μ΄λŸ¬ν•œ μ°¨μ΄λŠ” λͺ¨ν‚Ήμ— λŒ€ν•΄μ„œ λ°œμƒν•˜λŠ” 뢀뢄도 μžˆμŠ΅λ‹ˆλ‹€.
μˆ˜μ •ν•˜μ‹€ ν•„μš”λŠ” μ—†κ³ , μΈν„°νŽ˜μ΄μŠ€λ₯Ό λͺ¨ν‚Ήμ„ ν•˜λŠ”κ²ƒκ³Ό ν…ŒμŠ€νŠΈμš© κ΅¬ν˜„μ²΄λ₯Ό λ§Œλ“€μ–΄ ν…ŒμŠ€νŠΈ ν•˜λŠ” 것 두 가지에 λŒ€ν•΄ λ‚˜λ¦„λŒ€λ‘œμ˜ μ˜κ²¬μ„ μ •λ¦¬ν•΄λ³΄μ‹œλ©΄ 쒋을 것 κ°™μ•„μš”.

그리고 Kotest의 κ²½μš°λŠ” Mockk와 ν•¨κ»˜ μ‚¬μš©ν•˜λ©΄ μ„±λŠ₯μ €ν•˜κ°€ λ°œμƒν•˜κΈ°λ„ ν•©λ‹ˆλ‹€.
(λͺ¨ν‚Ήμ€ ν…ŒμŠ€νŠΈμ½”λ“œ λΉŒλ“œ μ‹œκ°„μ„ 였래 걸리게 λ§Œλ“œλŠ” μš”μ†Œ 쀑 ν•˜λ‚˜μž…λ‹ˆλ‹€.)
이런 λΆ€λΆ„μ—μ„œ μ˜€λŠ” μž₯단점도 κ³ λ €ν•  수 μžˆλ‹€λ©΄ 더 μ’‹κ² μŠ΅λ‹ˆλ‹€.


@MockK
private lateinit var fileManager: FileManager
Expand Down Expand Up @@ -93,7 +94,6 @@ class ExploreViewModelTest {
}
}
}

companion object {
val PDF_DUMMY = FileInfo(
name = "DUMMY.pdf",
Expand Down
Loading