Skip to content

Commit 9df3d42

Browse files
authored
Update README.md
1 parent e3233c9 commit 9df3d42

1 file changed

Lines changed: 67 additions & 4 deletions

File tree

README.md

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ class UserViewModel(private val dao: UserDao) : ViewModel() {
3838
viewModel.insert(1, "User").subscribeCompletable(this)
3939
//or with callback
4040
viewModel.insert(1, "Bobekos").subscribeCompletable(this,
41+
//optional
4142
onComplete = {
4243
showToast("User inserted")
4344
},
45+
//optional
4446
onError = {
4547
showToast(it.message)
4648
})
@@ -63,6 +65,7 @@ viewModel.getFromSingle(1).subscribeSingle(this,
6365
onSuccess = {
6466
showToast("User ${it.username} loaded")
6567
},
68+
//optional
6669
onError = {
6770
showToast(it.message)
6871
})
@@ -85,9 +88,11 @@ viewModel.getFromMaybe(1).subscribeMaybe(this,
8588
onSuccess = {
8689
showToast("User ${it.username} loaded")
8790
},
91+
//optional
8892
onError = {
8993
showToast(it.message)
9094
},
95+
//optional
9196
onComplete = {
9297
showToast("No user found")
9398
})
@@ -115,18 +120,76 @@ viewModel.loadUser().nonNullObserver(this,
115120
observer = {
116121
showToast("I'm observing ${it.username}")
117122
},
123+
//optional
118124
nullObserver = {
119125
showToast("Value is null")
120126
})
121127
```
122128

123-
## Features
129+
## Testing
124130

125-
...
131+
For each reactive source there is a specific test method.
132+
```kotlin
133+
liveData.testCompletableSubscribe(...)
134+
liveData.testMaybeSubscribe(...)
135+
liveData.testSingleSubscribe(...)
136+
```
126137

127-
## Testing
138+
Make sure to include the 'InstantTastExecutorRule' (core-testing) into your tests. Furthermore, the default IoSchedulerHandler (or the scheduler which you used) should be overwritten.
128139

129-
...
140+
```kotlin
141+
@RunWith(JUnit4::class)
142+
class UserViewModelTest {
143+
144+
private inline fun <reified T> lambdaMock(): T = Mockito.mock(T::class.java)
145+
146+
@get:Rule
147+
var rule: TestRule = InstantTaskExecutorRule()
148+
149+
private val userDao = mock(UserDao::class.java)
150+
private val viewModel = UserViewModel(userDao)
151+
152+
@Before
153+
fun setup() {
154+
RxJavaPlugins.setIoSchedulerHandler {
155+
Schedulers.trampoline()
156+
}
157+
}
158+
159+
@Test
160+
fun testGetFromSingleSuccess() {
161+
val testObject = UserEntity(1, "Bobekos")
162+
163+
`when`(userDao.getByIdAsSingle(1)).then { Single.just(testObject) }
164+
165+
val lifecycle = LifecycleRegistry(mock(LifecycleOwner::class.java))
166+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
167+
168+
val observer = lambdaMock<(t: UserEntity) -> Unit>()
169+
170+
viewModel.getFromSingle(1).testSingleSubscribe(lifecycle, onSuccess = observer)
171+
172+
verify(observer).invoke(testObject)
173+
}
174+
175+
@Test
176+
fun testGetFromSingleError() {
177+
val testObject = SQLiteConstraintException()
178+
179+
`when`(userDao.getByIdAsSingle(1)).then { Single.error<UserEntity>(testObject) }
180+
181+
val lifecycle = LifecycleRegistry(mock(LifecycleOwner::class.java))
182+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
183+
184+
val observer = lambdaMock<(e: Throwable) -> Unit>()
185+
186+
viewModel.getFromSingle(1).testSingleSubscribe(lifecycle, onError = observer)
187+
188+
verify(observer).invoke(testObject)
189+
}
190+
```
191+
192+
For more tests look into the sample [app](https://github.com/bobekos/ReactiveLiveData/blob/master/app/src/test/java/com/github/bobekos/rxviewmodelexample/viewmodel/UserViewModelTest.kt)
130193

131194
## Resources and Credits
132195

0 commit comments

Comments
 (0)