Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Created May 29, 2019 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamMc331/4fc8323e404e91531b1c30fc1bde403c to your computer and use it in GitHub Desktop.
Save AdamMc331/4fc8323e404e91531b1c30fc1bde403c to your computer and use it in GitHub Desktop.
class QuestionViewModel {
private val state = MutableLiveData<QuestionState>()
private val currentQuestion: LiveData<Question> = Transformations.map(state) {
(it as? QuestionState.Loaded)?.data
}
val questionTitle: String
get() = currentQuestion.value?.title.orEmpty
}
class QuestionViewModelTest {
@JvmField
@Rule
val instantExecutor = InstantTaskExecutorRule()
@Test
fun assertTitle() {
// Mock data load
assertEquals("TestTitle", viewModel.questionTitle)
// The above is false, saying viewModel.questionTitle is empty, because `currentQuestion` value is null.
// This is as if the Transformations.map() never ran
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment