Dear @osuleymanova
Please consider updating the solution in the codelab to the one below. It works, and the test passes provided that, the GameUiState data class is initialized with the property currentWordCount:Int = 1, not zero, as other commenters also noted.
@test
fun gameViewModel_AllWordsGuessed_UiStateUpdatedCorrectly() {
var expectedScore = 0
repeat(MAX_NO_OF_WORDS) {
val currentGameUiState = viewModel.uiState.value
val correctPlayerWord = getUnscrambledWord(currentGameUiState.currentScrambledWord)
expectedScore += SCORE_INCREASE
viewModel.updateUserGuess(correctPlayerWord)
viewModel.checkUserGuess()
//Assert that after each correct answer, score is updated correctly
assertEquals(expectedScore, viewModel.uiState.value.score)
}
val currentGameUiState = viewModel.uiState.value
//Assert that after all questions are answered, the current word count is up-to-date
assertEquals(MAX_NO_OF_WORDS, currentGameUiState.currentWordCount)
// Assert that after 10 questions are answered, the game is over.
assertTrue(currentGameUiState.isGameOver)
}
in the GameViewModel.kt the function updateGameState should be like in the main brach of your code, not like in the viewmodel branch.
private fun updateGameState(updatedScore: Int) {
if (usedWords.size == MAX_NO_OF_WORDS) {
//last round in the game, update isGameOver to true, don't pick a new word
_uiState.update { currentState ->
currentState.copy(
isGuessedWordWrong = false,
score = updatedScore,
isGameOver = true
)
}
} else {
//normal round
_uiState.update { currentState ->
currentState.copy(
isGuessedWordWrong = false,
currentScrambledWord = pickRandomAndShuffle(),
currentWordCount = currentState.currentWordCount.inc(),
score = updatedScore
)
}
}
}