Vivek Modi
07/05/2021, 1:51 PMviewModel.onDownloadErrorLiveData.observe(this, { consumable ->
some code
})
Matt Thompson
07/05/2021, 8:30 PMcapture
in a verify block or in an answers
block. Then just manually invoke the observer.Vivek Modi
07/05/2021, 8:35 PMMatt Thompson
07/05/2021, 8:39 PM```lateinit var observer : Observer
every { mockLiveData.observe(any(), any()) } answers { observer = secondArg() as Observer }
Vivek Modi
07/15/2021, 4:15 PMprivate fun yo(){
viewModel.consentTag.observe(this, Observer { type ->
typeText.text = type
})
}
this is my functionimport androidx.lifecycle.Observer
which import i need to use@Test
fun `yo `() {
every { mockViewModel.consentTag.observe(any(), any()) } answers {
observer = secondArg() as Observer
}
}
it giving errorMatt Thompson
07/15/2021, 4:17 PMObserver<TypeHere>
Vivek Modi
07/15/2021, 4:19 PMMatt Thompson
07/15/2021, 4:22 PMas
you also need to define the typesecondArg
takes a type argument, you could try putting it thereVivek Modi
07/15/2021, 4:24 PMobserver = secondArg() as ConsentType
Matt Thompson
07/15/2021, 4:24 PMas Observer<ConsentType>
observe
function is Observer<ConsentType>
- kotlin allows you to write it as a lambda with SAM conversionVivek Modi
07/15/2021, 4:28 PMprivate fun yo(){
viewModel.consentTag.observe(this, Observer { type ->
typeText.text = type
})
}
how can i set value type
in typeTextMatt Thompson
07/15/2021, 4:29 PMonChanged
Vivek Modi
07/15/2021, 4:34 PM@Test
fun `yo `() {
val value = ConsentType.GENERAL
every { mockViewModel.consentTag.observe(any(), any()) } answers {
observer = secondArg() as Observer<ConsentType>
}
observer.onChanged(value)
consentActivity.yo()
verify { mockTextView.tag = value }
}
kotlin.UninitializedPropertyAccessException: lateinit property observer has not been initialized
Matt Thompson
07/15/2021, 6:36 PMevery
/ answers
works is that the answers
block with be run every time the function inside of every
gets called. so if this yo
function is what calls it, you need to call it in order to initialize your observer
varVivek Modi
07/15/2021, 6:46 PMyo
at top of function ?