google-developer-training / basic-android-kotlin-compose-training-mars-photos

Solution code for Android Basics in Kotlin course
https://developer.android.com/courses/android-basics-kotlin/course?gclid=CjwKCAjw4c-ZBhAEEiwAZ105RTyT-iaLHzrhMBUXdMhO230ZDwOwxxI2x4RgK8DwBxK8t1h0wmU_QxoCi4YQAvD_BwE
Apache License 2.0
66 stars 58 forks source link

Add repository and Manual DI #24

Open tugceaktepe opened 1 year ago

tugceaktepe commented 1 year ago

URL of codelab: https://developer.android.com/codelabs/basic-android-kotlin-compose-add-repository?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-5-pathway-2%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-add-repository#9

In which task and step of the codelab can this issue be found? Task 10 - Step 10 - To apply this rule to your tests, add the @get:Rule annotation to the testDispatcher property.

Describe the problem After add TestDispatcherRule, it's expected to pass unit test, but it fails because of excpected value. In step 5, expected value is given like this:

@Test
fun marsViewModel_getMarsPhotos_verifyMarsUiStateSuccess() =
   runTest {
       val marsViewModel = MarsViewModel(
           marsPhotosRepository = FakeNetworkMarsPhotosRepository()
       )
       assertEquals(
           MarsUiState.Success("Success: ${FakeDataSource.photosList.size} Mars " +
                   "photos retrieved"),
           marsViewModel.marsUiState
       )
   }

However, actual value is MarsUiState.Success("Success. ${listResult.size} Mars photos retrieved") in MarsViewModel. So expected value should be with "." instead of ":" in step 5.

Or vice versa, it can be fixed with MarsUiState.Success("Success: ${listResult.size} Mars photos retrieved") in MarsViewModel in repo-starter branch .

Steps to reproduce?

  1. Go to... from step 5 to step 10
  2. Click on... after add TestDispatcherRule , run unit test
  3. See error...
expected:<Success(photos=Success: 2 Mars photos retrieved)> but was:<Success(photos=Success. 2 Mars photos retrieved)>
 Expected :Success(photos=Success: 2 Mars photos retrieved)
 Actual   :Success(photos=Success. 2 Mars photos retrieved)
 <Click to see difference>

java.lang.AssertionError: expected:<Success(photos=Success: 2 Mars photos retrieved)> but was:<Success(photos=Success. 2 Mars photos retrieved)>
    at org.junit.Assert.fail(Assert.java:89)
    at org.junit.Assert.failNotEquals(Assert.java:835)
    at org.junit.Assert.assertEquals(Assert.java:120)
    at org.junit.Assert.assertEquals(Assert.java:146)
    at com.example.marsphotos.ui.screens.MarsViewModelTest$marsViewModel_getMarsPhotos_verifyMarsUiStateSuccess$1.invokeSuspend(MarsViewModelTest.kt:22)

Versions Android Studio version: API version of the emulator:

Additional information Include screenshots if they would be useful in clarifying the problem.