I need to write the test for Fragment and I need it takes a data from fake repository. I created fake Fragment for test with val fragmentScenario = launchFragmentInContainer and initialized repository with a fake repository but if I just use real view model it is try to take a data from real repository. So I guess I have to use fake ViewModel to take a data from fake repository. But I didnt figured out how to fake it. How can I fake it?
@RunWith(AndroidJUnit4::class)
@ExperimentalCoroutinesApi
class WeatherForecastFragmentTest {
private lateinit var weatherRepository: WeatherRepository
private lateinit var locationRepository: LocationRepository
private lateinit var weatherViewModel: WeatherForecastViewModel
@Before
fun initRepository() = runTest {
locationRepository = FakeAndroidLocationRepository()
weatherRepository = FakeAndroidWeatherRepository()
weatherViewModel = WeatherForecastViewModel(weatherRepository, locationRepository)
cleanDb()
}
@Test
fun initViewModel_loadListWeather_DisplayedInUi() = runBlockingTest {
val location = Location(1, "Moscow")
val bundle = WeatherForecastFragmentArgs(location.id, location.name).toBundle()
val fragmentScenario = launchFragmentInContainer<WeatherForecastFragment>(
bundle, R.style.ThemeOverlay_AppCompat_Light
)
Espresso.onView(ViewMatchers.withId(R.id.weatherForecastRecyclerView))
.check(matches(isDisplayed()))```