forked from karntrehan/Posts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENH] Tested ViewModel, local data source, improved injections
- Loading branch information
1 parent
c9d55fe
commit b3543e2
Showing
12 changed files
with
122 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
posts/src/test/java/com/karntrehan/posts/list/viewmodel/ListViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.karntrehan.posts.list.viewmodel | ||
|
||
import android.arch.lifecycle.Observer | ||
import com.karntrehan.posts.commons.DummyData | ||
import com.karntrehan.posts.commons.data.PostWithUser | ||
import com.karntrehan.posts.list.model.ListDataContract | ||
import com.mpaani.core.networking.Outcome | ||
import com.nhaarman.mockito_kotlin.doReturn | ||
import com.nhaarman.mockito_kotlin.mock | ||
import com.nhaarman.mockito_kotlin.verify | ||
import com.nhaarman.mockito_kotlin.whenever | ||
import io.reactivex.disposables.CompositeDisposable | ||
import io.reactivex.subjects.PublishSubject | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import java.io.IOException | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class ListViewModelTest { | ||
|
||
private lateinit var viewModel: ListViewModel | ||
|
||
private val repo: ListDataContract.Repository = mock() | ||
|
||
private val outcome: Observer<Outcome<List<PostWithUser>>> = mock() | ||
|
||
@Before | ||
fun init() { | ||
viewModel = ListViewModel(repo, CompositeDisposable()) | ||
whenever(repo.postFetchOutcome).doReturn(PublishSubject.create()) | ||
viewModel.postsOutcome.observeForever(outcome) | ||
} | ||
|
||
@Test | ||
fun testGetPostsSuccess() { | ||
viewModel.getPosts() | ||
verify(repo).fetchPosts() | ||
|
||
repo.postFetchOutcome.onNext(Outcome.loading(true)) | ||
verify(outcome).onChanged(Outcome.loading(true)) | ||
|
||
repo.postFetchOutcome.onNext(Outcome.loading(false)) | ||
verify(outcome).onChanged(Outcome.loading(false)) | ||
|
||
val data = listOf(DummyData.PostWithUser(1), DummyData.PostWithUser(2)) | ||
repo.postFetchOutcome.onNext(Outcome.success(data)) | ||
verify(outcome).onChanged(Outcome.success(data)) | ||
} | ||
|
||
@Test | ||
fun testGetPostsError() { | ||
val exception = IOException() | ||
repo.postFetchOutcome.onNext(Outcome.failure(exception)) | ||
verify(outcome).onChanged(Outcome.failure(exception)) | ||
} | ||
|
||
@Test | ||
fun testRefreshPosts() { | ||
viewModel.refreshPosts() | ||
verify(repo).refreshPosts() | ||
} | ||
} |