Last active
October 13, 2018 14:36
-
-
Save rsetkus/acbd9f758fc199ff2d2eaaecf8621174 to your computer and use it in GitHub Desktop.
InstantTaskExecutorRule
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
class SuperHeroesViewModelTest { | |
@get:Rule | |
val instantTaskExecutorRule = InstantTaskExecutorRule() | |
private val superHeroesRepository = mockk<SuperHeroesRepository>() | |
private lateinit var superHeroesViewModel: SuperHeroesViewModel | |
private val listOfSuperHeroes = listOf(SuperHero("Iron Man"), SuperHero("Hulk")) | |
private val successfulSuperHeroesResult = Result.Success(listOfSuperHeroes) | |
@Before | |
fun setUp() { | |
every { superHeroesRepository.loadSuperHeroes() } returns successfulSuperHeroesResult | |
superHeroesViewModel = SuperHeroesViewModel(superHeroesRepository) | |
} | |
@Test | |
fun `when tasks are received from repository then should load to the view`() { | |
with(superHeroesViewModel) { | |
loadSuperHeroes() | |
verify { superHeroesRepository.loadSuperHeroes() } | |
val receivedSuperHeroes = superHeroesLiveData.testObserver().observedValues.get(0) | |
assertThat(receivedSuperHeroes).isNotEmpty().hasSize(listOfSuperHeroes.size) | |
} | |
} | |
} |
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
class SuperHeroesViewModelTest { | |
@get:Rule | |
var instantExecutorRule = InstantTaskExecutorRule() | |
@Mock | |
private lateinit var superHeroesRepository: SuperHeroesRepository | |
private lateinit var superHeroesViewModel: SuperHeroesViewModel | |
@Before | |
fun setUp() { | |
MockitoAnnotations.initMocks(this) | |
superHeroesViewModel = SuperHeroesViewModel(superHeroesRepository) | |
} | |
@Test | |
fun `when tasks loaded from repository then should delegate to view callback`() { | |
with(superHeroesViewModel) { | |
loadSuperHeroes() | |
verify(superHeroesRepository).loadSuperHeroes() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment