Created
June 29, 2019 17:56
-
-
Save jeremyrempel/e869177f0ac462e098f4d910d9829334 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(AndroidJUnit4::class) | |
class MainFragmentTest { | |
| |
@MockK | |
private lateinit var fakeViewModel: MainFragmentViewModel | |
| |
@Before | |
fun setUp() = MockKAnnotations.init(this) | |
| |
@Test | |
fun `testFragmentUpdateData`() { | |
// mock the viewmodel behavior | |
every { fakeViewModel.isLoading() } returns MutableLiveData(false) | |
every { fakeViewModel.getData() } returns MutableLiveData("Hello World from Mock") | |
| |
// use androidx.test to launch fragment in isolation and inject in mocked ViewModel | |
launchFragmentInContainer<MainFragment>( | |
Bundle.EMPTY, | |
R.style.FragmentScenarioEmptyFragmentActivityTheme, | |
FragFactoryFake(fakeViewModel) | |
) | |
| |
// verify results | |
onView(withId(R.id.loadingView)) | |
.check(matches(withEffectiveVisibility(Visibility.GONE))) | |
onView(withId(R.id.textView)) | |
.check(matches(withText("Hello World from Mock"))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment