Skip to content

Commit

Permalink
Remove DaggerComponent usage in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume M committed Oct 14, 2019
1 parent cb7848d commit 2ca690c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@ package io.freshdroid.mymonzo.viewmodels

import android.net.Uri
import io.freshdroid.mymonzo.MyMonzoRobolectricTestCase
import io.freshdroid.mymonzo.core.user.CurrentUserType
import io.freshdroid.mymonzo.navigation.ApplicationMap
import io.freshdroid.mymonzo.splashscreen.SplashScreenEnvironment
import io.freshdroid.mymonzo.splashscreen.di.DaggerSplashScreenComponent
import io.freshdroid.mymonzo.splashscreen.di.SplashScreenComponent
import io.freshdroid.mymonzo.splashscreen.viewmodels.SplashScreenViewModel
import io.reactivex.Scheduler
import io.reactivex.schedulers.TestScheduler
import io.reactivex.subscribers.TestSubscriber
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.mock
import java.util.concurrent.TimeUnit

internal class SplashScreenViewModelTest : MyMonzoRobolectricTestCase() {

private lateinit var component: SplashScreenComponent
private lateinit var environment: SplashScreenEnvironment

@Before
fun initComponent() {
component = DaggerSplashScreenComponent.builder().coreComponent(coreComponent()).build()
environment = SplashScreenEnvironment(
mock(CurrentUserType::class.java),
mock(Scheduler::class.java)
)
}

private fun environment(): SplashScreenEnvironment = component.environment()

@Test
fun testLaunchNextActivity() {
val testScheduler = TestScheduler()
val environment = environment().copy(
val environment = environment.copy(
scheduler = testScheduler
)
val vm = SplashScreenViewModel(environment, scopeProvider())
val vm = SplashScreenViewModel(environment)
val launchNextActivity = TestSubscriber<Uri>()
vm.outputs.launchNextActivity().subscribe(launchNextActivity::onNext)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
package io.freshdroid.mymonzo.feed.viewmodels

import io.freshdroid.mymonzo.core.network.ErrorEnvelope
import io.freshdroid.mymonzo.core.user.CurrentUserType
import io.freshdroid.mymonzo.feed.FeedEnvironment
import io.freshdroid.mymonzo.feed.MyMonzoRobolectricTestCase
import io.freshdroid.mymonzo.feed.di.DaggerFeedComponent
import io.freshdroid.mymonzo.feed.di.FeedComponent
import io.freshdroid.mymonzo.feed.exceptions.ApiExceptionFactory
import io.freshdroid.mymonzo.feed.factories.BalanceFactory
import io.freshdroid.mymonzo.feed.mocks.MockApiFeed
import io.freshdroid.mymonzo.feed.models.Balance
import io.freshdroid.mymonzo.feed.network.ApiFeedType
import io.reactivex.Observable
import io.reactivex.Scheduler
import io.reactivex.subscribers.TestSubscriber
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.mock

internal class FeedFragmentViewModelTest : MyMonzoRobolectricTestCase() {

private lateinit var _component: FeedComponent
private lateinit var environment: FeedEnvironment

@Before
fun initComponent() {
_component = DaggerFeedComponent.builder().coreComponent(coreComponent()).build()
environment = FeedEnvironment(
mock(CurrentUserType::class.java),
mock(ApiFeedType::class.java),
mock(Scheduler::class.java)
)
}

private fun environment(): FeedEnvironment = _component.environment()

@Test
fun testCurrentBalance() {
val environment = environment().copy(
val environment = environment.copy(
apiFeed = object : MockApiFeed() {
override fun fetchBalance(): Observable<Balance> {
return Observable.just(BalanceFactory.creator())
Expand All @@ -44,7 +48,7 @@ internal class FeedFragmentViewModelTest : MyMonzoRobolectricTestCase() {

@Test
fun testFetchBalanceApiError() {
val environment = environment().copy(
val environment = environment.copy(
apiFeed = object : MockApiFeed() {
override fun fetchBalance(): Observable<Balance> {
return Observable.error(ApiExceptionFactory.badRequestException())
Expand Down

0 comments on commit 2ca690c

Please sign in to comment.