awesome tutorial, i’m trying the work manager tutorial and testing that particular component is very hard, since the big dependency in the android sdk and async nature of the work manager, where can i find some help, tutorial or any resource for testing code with dependency on the work manager?
thanks in advance
Thanks!
Oh that’s a great question! I haven’t gotten a change to work with the Work Manager yet, so I’m not sure off the top of my head. My first thoughts are working with an integration test, or maybe with Roboletric. I think what I default to trying first is abstracting what I can into helper classes and test those.
It looks like @fernandospr wrote the tutorial on Work Manager. Have you seen much on how it can be tested?
Awesome tutorial. Let helped me understand TDD. I have a question, though. What’s the approach to follow when you already have an existing codebase? How do you start adding tests?
tl;dr I’d say is before you start working on something in your existing code, write I tests for that behavior first, whether it’s to keep the same behavior you already have (test should start green and stay green), or writing a test for new/changed behavior.
When running your tests, I’m getting this error:
Wanted but not invoked:
victoryRepository.getVictoryCount();
→ at com.raywenderlich.android.smallvictories.VictoryViewModelTest.incrementVictoryCountCallsRepository(VictoryViewModelTest.kt:50)
Actually, there were zero interactions with this mock.
Oh no! Thanks for pointing this out, @igorganapolsky. I’ll see if I can reproduce this. Are you seeing in the final project attachment, or while walking through the tutorial (I’m guessing you’ve reached the heading “Writing a UI Test” when you’re seeing this error?)
I completed the challenge for the reset button. Perhaps you might want to include that in your tutorial: MainActivityTest @Test
fun tappingResetChangesCount() {
// Given
onView(withId(R.id.fab))
.perform(click())
// When
onView(withText(R.string.action_reset))
.perform(click())
// Then
onView(allOf(withId(R.id.textVictoryCount), withText("0")))
.check(matches(isDisplayed()))
}
VictoryViewModelTest @Test
fun resetClearsCount() {
// Given
val count = 5
stubVictoryRepositoryGetVictoryCount(count)
// When
viewModel.reset()
// Then
verify(mockVictoryRepository).setVictoryCount(count) // Assert
}
VictoryViewModel
fun reset() {
repository.clear()
val newCount = repository.getVictoryCount()
repository.setVictoryCount(newCount)
viewState.value = VictoryUiModel.CountUpdated(newCount)
}