-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package kr.co.testing.dummy | ||
|
||
import kr.co.model.FileInfo | ||
import kr.co.model.FileInfo.Type.PDF | ||
import java.time.LocalDateTime | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package kr.co.testing.repository | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kr.co.data.repository.BookmarkRepository | ||
import kr.co.model.FileInfo | ||
|
||
class TestBookmarkRepository: BookmarkRepository { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런 특정 도메인/피처와 연관되는 테스트 클래스를 해당 모듈 내에 위치하는 것이 좋을 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. data모듈에 repsitory interface가 분리되어 data모듈만 바라봐서 모든 피처 모듈은 몰라도 괜찮지 않나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (정정 : feature 모듈 -> data 모듈) |
||
|
||
private val bookmarkFilesFlow: MutableStateFlow<List<FileInfo>> = | ||
MutableStateFlow(emptyList()) | ||
|
||
override suspend fun insert(bookmarkFile: FileInfo) { | ||
bookmarkFilesFlow.update { it + bookmarkFile } | ||
} | ||
|
||
override fun get(): Flow<List<FileInfo>> = | ||
bookmarkFilesFlow | ||
|
||
override suspend fun delete(bookmarkFile: FileInfo) { | ||
bookmarkFilesFlow.update { it - bookmarkFile } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package kr.co.bookmark | ||
|
||
import app.cash.turbine.test | ||
import kotlinx.coroutines.test.runTest | ||
import kr.co.model.BookmarkSideEffect | ||
import kr.co.model.BookmarkUiIntent | ||
import kr.co.testing.dummy.PDF_DUMMY | ||
import kr.co.testing.repository.TestBookmarkRepository | ||
import kr.co.testing.repository.TestRecentRepository | ||
import kr.co.testing.rule.CoroutineTestRule | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
internal class BookmarkViewModelTest { | ||
|
||
@get: Rule | ||
val coroutineTestRule = CoroutineTestRule() | ||
|
||
private val bookmarkRepository = TestBookmarkRepository() | ||
private val recentRepository = TestRecentRepository() | ||
|
||
private lateinit var viewModel: BookmarkViewModel | ||
|
||
@Before | ||
fun setup() { | ||
viewModel = BookmarkViewModel(bookmarkRepository,recentRepository) | ||
} | ||
|
||
@Test | ||
fun `Given a Unit when Init intent is handled then state is updated`() = runTest { | ||
viewModel.handleIntent(BookmarkUiIntent.Init) | ||
|
||
viewModel.uiState.test { | ||
val state = awaitItem() | ||
assert(state.files.isEmpty()) | ||
} | ||
} | ||
|
||
@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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아래와 같이 작성하면 스마트 캐스팅을 활용할 수 있습니다.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. require를 활용한 방법은 어떤가요 require(this is BookmarkSideEffect.NavigateToPdf)
assert(path == file.path) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 테스트 코드에서 require는 적합하지 않습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 전 assert로 왜 스마트 캐스팅이 안되죠,, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 음? 그럼 혹시 assertTrue(effect is BookmarkSideEffect.NavigateToPdf) 로 해보시겠어요? |
||
assert((it as BookmarkSideEffect.NavigateToPdf).path == file.path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assertEquals를 사용하면 의미를 더 정확하게 표현할 수 있습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다음 PR에서 수정했습니다 |
||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
테스트용 데이터를 공유해야하는 이유가 궁금합니다.
A 피처 모듈, B 피처 모듈에서 이것으로 동일하게 테스트를 한다면, 만약 누군가 테스트 데이터를 변경하면 다른 모듈의 테스트 코드가 실패할 수 있을 것 같습니다.
변경된 파일만 테스트를 한다면 이러한 테스트 코드 실패를 늦게 발견하게 될 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수정하겠습니다