Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Basics: Viewmodel and State with Compose #213

Open
Ganesh2914 opened this issue Sep 24, 2024 · 0 comments
Open

Android Basics: Viewmodel and State with Compose #213

Ganesh2914 opened this issue Sep 24, 2024 · 0 comments

Comments

@Ganesh2914
Copy link

Ganesh2914 commented Sep 24, 2024

Test Strategy for Success path
https://developer.android.com/codelabs/basic-android-kotlin-compose-test-viewmodel?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-4-pathway-1%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-test-viewmodel#3

Hey developers, On facing an issue in the code this might be the solution,
var currentGameUiState = viewModel.uiState.value
val correctPlayerWord = getUnscrambledWord(currentGameUiState.currentScrambledWord)

The currentGameUiState.currentScrambledWord is generated by the gameViewModel for example : "omeehmda"

getUnscrambledWord method is from test/.../data file where it maps the wordLength from allWords set and give you the value. Here the currentGameUiState.currentScrambledWord length is 8 and the getUnscrambledWord maps it and gives you an 8 Length word which is for example: birthday from test/../data file

The gameViewModel code is right, So the currentGameUiState.isGuessedWordWrong is true which is given by the gameViewModel.

currentGameUiState = viewModel.uiState.value
// Assert that checkUserGuess() method updates isGuessedWordWrong is updated correctly.
assertFalse(currentGameUiState.isGuessedWordWrong)
// Assert that score is updated correctly.
assertEquals(SCORE_AFTER_FIRST_CORRECT_ANSWER, currentGameUiState.score)
Here what is the point of using assertFalse where the currentGameUiState.isGuessedWordWrong is true and ends
up with assertionError and assertEquals also fails because correctPlayerWord is not true and the score wouldn't be increased.

To fix the code
assertTrue(currentGameUiState.isGuessedWordWrong)
// Assert that score is updated correctly
assertEquals(Wrong_Answer, currentGameUiState.Score)
companion object {
private const val SCORE_AFTER_FIRST_CORRECT_ANSWER = SCORE_INCREASE
private const val Worng_Answr= Wrong_Answer
}
}

ViewModel Problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant