Basically, how do i test a function like this ```f...
# announcements
p
Basically, how do i test a function like this
Copy code
fun foo(callback){
  GlobalScope.launch {
    val x = async {//}.await()
    callback(x)
  }
}

//Test
@Test
`test that we can test`(){
   //i should be able to test that callback is called with value x
}
e
make callback type (T) -> Unit where T is your type, and it can take println in the test
e
In general prefer not to use GlobalScope, but instead make the scope a dependency so you can test using e.g. TestCoroutineScope. But to verify that the callback was invoked, just pass a test callback that sets some asserted value. If this question is #C1CFAFJSK specific, then better ask there! #CGN6QTTFE is also an option.
👍 1
p
@Erik Yeah, i'm passing a test callback, that is a mock of the callback that i can test...
Copy code
val callback = mockk<StreamObserver<Wallet>>()
        val slot = slot<Wallet>()
        every { callback.onNext(capture(slot)) } just Runs
        every { callback.onCompleted() } just Runs
i can ascertain within the
foo
function that the code within launch block runs.. However it seems that my test function runs to completion before the callback..
@Erik Thanks, it works now... so i had to inject the scope like you said...

https://youtu.be/hMFwNLVK8HU?t=975

I saw this video as well...