https://kotlinlang.org logo
#mockk
Title
# mockk
v

Vivek Modi

07/05/2021, 1:51 PM
Hey i want to test observer in mockk. Anyone know how to do that.
Copy code
viewModel.onDownloadErrorLiveData.observe(this, { consumable ->
           some code
        })
m

Matt Thompson

07/05/2021, 8:30 PM
You can get a reference to arguments passed in with
capture
in a verify block or in an
answers
block. Then just manually invoke the observer.
v

Vivek Modi

07/05/2021, 8:35 PM
@Matt Thompson thanks for your reply. I am new in this library. I didn't get the reference or example. Can you please give me the example if possible. It would be great pleasure for me.
m

Matt Thompson

07/05/2021, 8:39 PM
Sorry not on a proper keyboard but I'll try...
Copy code
```lateinit var observer : Observer
every { mockLiveData.observe(any(), any()) } answers { observer = secondArg() as Observer }
something like that
v

Vivek Modi

07/15/2021, 4:15 PM
@Matt Thompson
Copy code
private fun yo(){
    viewModel.consentTag.observe(this, Observer { type ->
        typeText.text = type
    })
}
this is my function
Copy code
import androidx.lifecycle.Observer
which import i need to use
Copy code
@Test
fun `yo `() {
    every { mockViewModel.consentTag.observe(any(), any()) } answers {
        observer = secondArg() as Observer
    }

}
it giving error
m

Matt Thompson

07/15/2021, 4:17 PM
you need to add the generic type of the LiveData
i.e.
Observer<TypeHere>
v

Vivek Modi

07/15/2021, 4:19 PM
okk
it fixed on complaining in observer issue
it again complaining this @Matt Thompson
ConsentType is enum class
m

Matt Thompson

07/15/2021, 4:22 PM
yeah when you cast it using
as
you also need to define the type
or alternatively I believe
secondArg
takes a type argument, you could try putting it there
v

Vivek Modi

07/15/2021, 4:24 PM
Sorry for again asking this
Copy code
observer = secondArg() as ConsentType
like this ?
m

Matt Thompson

07/15/2021, 4:24 PM
no,
as Observer<ConsentType>
the actual type of the lambda that you’re passing in as the second argument of the
observe
function is
Observer<ConsentType>
- kotlin allows you to write it as a lambda with SAM conversion
v

Vivek Modi

07/15/2021, 4:28 PM
and little bit confuse
Copy code
private fun yo(){
    viewModel.consentTag.observe(this, Observer { type ->
        typeText.text = type
    })
}
how can i set value
type
in typeText
m

Matt Thompson

07/15/2021, 4:29 PM
once you have the reference to the observer you can manually invoke
onChanged
v

Vivek Modi

07/15/2021, 4:34 PM
@Matt Thompson like this
Copy code
@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 }
    }
it gives error
kotlin.UninitializedPropertyAccessException: lateinit property observer has not been initialized
m

Matt Thompson

07/15/2021, 6:36 PM
yeah that makes sense - the way
every
/
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
var
v

Vivek Modi

07/15/2021, 6:46 PM
So i need to call
yo
at top of function ?
2 Views