Last active
November 26, 2017 05:10
-
-
Save tolmachevroman/804e9cce0e44b45c4648c6cd423e4687 to your computer and use it in GitHub Desktop.
Medium Post 3. RestaurantsViewModelTest
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
@RunWith(JUnit4::class) | |
class RestaurantsViewModelTest { | |
@Rule | |
@JvmField | |
var instantExecutor = InstantTaskExecutorRule() | |
@Mock | |
lateinit var repository: RestaurantsRepository | |
@Mock | |
lateinit var observer: Observer<Resource<List<Restaurant>>> | |
lateinit var restaurantsViewModel: RestaurantsViewModel | |
@Before | |
fun setup() { | |
MockitoAnnotations.initMocks(this) | |
restaurantsViewModel = RestaurantsViewModel(repository) | |
} | |
/** | |
* When cuisineInput is set to 1, return all restaurants where cuisine param is 1 | |
*/ | |
@Test | |
fun getRestaurantsByCuisineFromRepository() { | |
val restaurant1 = Restaurant(1, 1, "Fake Restaurant", 0.0, 0.0, 1000, | |
"image", "description") | |
val restaurant3 = Restaurant(3, 1, "Fake Restaurant 3", 0.0, 0.0, 1000, | |
"image", "description") | |
val restaurants = MutableLiveData<Resource<List<Restaurant>>>() | |
val resource = Resource.success(listOf(restaurant1, restaurant3)) | |
restaurants.value = resource | |
Mockito.`when`(repository.getRestaurants(1)).thenReturn(restaurants) | |
restaurantsViewModel.restaurants.observeForever(observer) | |
Assert.assertFalse(restaurantsViewModel.initialized) | |
restaurantsViewModel.cuisineInput.value = 1 | |
Assert.assertTrue(restaurantsViewModel.initialized) | |
verify(observer).onChanged(ArgumentMatchers.refEq(resource)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment