Skip to content

Instantly share code, notes, and snippets.

@PhilippeBoisney
Created August 12, 2018 09:52
Show Gist options
  • Save PhilippeBoisney/ea6018761004644f6ed1d345eb0a14d2 to your computer and use it in GitHub Desktop.
Save PhilippeBoisney/ea6018761004644f6ed1d345eb0a14d2 to your computer and use it in GitHub Desktop.
class UserUnitTest : BaseTest() {
...
// TESTS
@Test
fun getUser_whenSuccess() {
// Prepare data
this.mockHttpResponse("getUser_whenSuccess.json", HttpURLConnection.HTTP_OK)
// Pre-test
assertEquals(null, this.viewModel.user.value, "User should be null because stream not started yet")
// Execute View Model
this.viewModel.getUser()
// Checks
assertEquals(EXPECTED_USER, this.viewModel.user.value, "User must be fetched")
assertEquals(false, this.viewModel.isLoading.value, "Should be reset to 'false' because stream ended")
assertEquals(null, this.viewModel.errorMessage.value, "No error must be founded")
}
@Test
fun getUser_whenError(){
// Prepare data
this.mockHttpResponse("getUser_whenSuccess.json", HttpURLConnection.HTTP_BAD_GATEWAY)
// Pre-test
assertEquals(null, this.viewModel.user.value, "User should be null because stream not started yet")
// Execute View Model
this.viewModel.getUser()
// Checks
assertEquals(null, this.viewModel.user.value, "User must be null because of http error")
assertEquals(false, this.viewModel.isLoading.value, "Should be reset to 'false' because stream ended")
assertNotEquals(null, this.viewModel.errorMessage.value, "Error value must not be empty")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment