Merge pull request #52 from fknives/review-viewmodel

Proof read viewmodel instruction set
This commit is contained in:
Gergely Hegedis 2022-01-28 13:57:27 +02:00 committed by GitHub
commit d29207be12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -52,7 +52,7 @@ fun setUp() {
### 1. `loggedOutUserGoesToAuthentication` ### 1. `loggedOutUserGoesToAuthentication`
We want to thest that if the user is not logged in then we are navigated to the Authentication screen. We want to test that if the user is not logged in then we are navigated to the Authentication screen.
So we need to setup the mock's response: So we need to setup the mock's response:
```kotlin ```kotlin
@ -60,7 +60,7 @@ whenever(mockIsUserLoggedInUseCase.invoke()).doReturn(false)
``` ```
Next up we want to setup our TestObserver for LiveData. This enables us to verify the values sent into a LiveData. Next up we want to setup our TestObserver for LiveData. This enables us to verify the values sent into a LiveData.
If a livedata is not observed, it's value may not updated (like a livedata that maps) so it's important to have a proper TestObserver set. If a livedata is not observed, its value may not be updated (like a livedata that maps) so it's important to have a proper TestObserver set.
```kotin ```kotin
@ -69,7 +69,7 @@ val navigateToTestObserver = sut.navigateTo.test()
Since the action takes place in the ViewModel constructor, instead of additional calls, we need to simulate that time has elapsed. Since the action takes place in the ViewModel constructor, instead of additional calls, we need to simulate that time has elapsed.
Note: the Extension we are using StandardTestDispatcher, that's why our test is linear and not shaky. Note: the `TestMainDispatcher` Extension we are using sets `StandardTestDispatcher` as the dispatcher for `Dispatcher.Main`, that's why our test is linear and not shaky.
```kotlin ```kotlin
testScheduler.advanceTimeBy(501) testScheduler.advanceTimeBy(501)
@ -127,11 +127,11 @@ What it does is:
Let's open `org.fnives.test.showcase.ui.auth.CodeKataAuthViewModel`. Let's open `org.fnives.test.showcase.ui.auth.CodeKataAuthViewModel`.
The setup is already done because it's almost the same as menitoned in CodeKataSplashViewModelTest. The setup is already done because it's almost the same as mentioned in CodeKataSplashViewModelTest.
### 1. `initialSetup` ### 1. `initialSetup`
As always we start with the easiest test. This usually gives us motivitaion and helps us giving idea for the next tests. As always we start with the easiest test. This usually gives us motivation and helps us get ideas for the next tests.
First we setup the observers: First we setup the observers:
```kotlin ```kotlin
@ -189,8 +189,8 @@ navigateToHomeTestObserver.assertNoValue()
### 3. `whenUsernameChangedLiveDataIsUpdated` ### 3. `whenUsernameChangedLiveDataIsUpdated`
This is esentially the same as whenPasswordChangedLiveDataIsUpdated, just for the username, so try to do it on your own. This is essentially the same as whenPasswordChangedLiveDataIsUpdated, just for the username, so try to do it on your own.
However for completeness sake: However for the sake of completeness:
```kotlin ```kotlin
val usernameTestObserver = sut.username.test() val usernameTestObserver = sut.username.test()
@ -224,7 +224,7 @@ runBlocking {
} }
``` ```
`anyOrNull()` just means we do not care what is passed, any anything is accepted. `anyOrNull()` just means we do not care what is passed, anything is accepted.
Let's do the action: Let's do the action:
@ -275,7 +275,7 @@ sut.onUsernameChanged("usr")
testScheduler.advanceUntilIdle() testScheduler.advanceUntilIdle()
``` ```
Next we do our action and clikc the button: Next we do our action and click the button:
```kotlin ```kotlin
sut.onLogin() sut.onLogin()
testScheduler.advanceUntilIdle() testScheduler.advanceUntilIdle()