Skip to content

演習9.2 #11

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

Open
wants to merge 1 commit into
base: answer/9.1
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package jp.co.mixi.androidtraining.sns.data.repository

import jp.co.mixi.androidtraining.sns.data.entity.Post
import kotlinx.coroutines.delay

class FakeTimelineRepository(private val timeline: List<Post>) : TimelineRepository {
override suspend fun getTimeline(): List<Post> {
delay(2000)
return timeline
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package jp.co.mixi.androidtraining.sns.ui

import jp.co.mixi.androidtraining.sns.data.entity.Post
import jp.co.mixi.androidtraining.sns.data.entity.User
import jp.co.mixi.androidtraining.sns.data.repository.FakeTimelineRepository
import jp.co.mixi.androidtraining.sns.utils.MainDispatcherRule
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Rule
import org.junit.Test

class TimelineViewModelTest {

@get:Rule
val dispatcherRule = MainDispatcherRule()

private val fakeTimeline = listOf(
Post(author = User(name = "User Name", photoUrl = ""), content = "Content"),
)

private val viewModel = TimelineViewModel(FakeTimelineRepository(fakeTimeline))

@Test
fun testGetTimeline() = runTest {
assertEquals(emptyList<Post>(), viewModel.uiState.posts)
viewModel.getTimeline()
advanceUntilIdle()
assertEquals(fakeTimeline, viewModel.uiState.posts)
assertFalse(viewModel.uiState.isLoading)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package jp.co.mixi.androidtraining.sns.utils

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description

class MainDispatcherRule : TestWatcher() {
override fun starting(description: Description) {
Dispatchers.setMain(StandardTestDispatcher())
}

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