https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

rkeazor

09/16/2019, 11:41 PM
how do you guys test suspendCancellableCorourtine.
d

Dominaezzz

09/16/2019, 11:42 PM
Test?
r

rkeazor

09/16/2019, 11:46 PM
as in . is it possible to test what is captured by the continuation
g

gildor

09/17/2019, 2:57 AM
Why do you need this? just run suspend function and test result
r

rkeazor

09/17/2019, 11:31 AM
hmm let me give you an example
lets say you have a function like this fun getUser: User { suspendCancellableCoroutine<User> { cont -> addCallback( object : Callback { override onSuccess(user: User) { if (user != null) cont.resume(user) else cancel (Exception ("No User Found")
sorry for the psuedoCode. now as you can see , how will you test the onSuccessMethod here
the onSuccesss method has two test paths
d

Dominaezzz

09/17/2019, 11:42 AM
Call
getUser
add trigger the callback for both paths in your tests then.
r

rkeazor

09/17/2019, 11:45 AM
so what about the continuation than? for instance you could use argumentCapture to capture the what is captured in the callback. but you also need a instance of the continuation so you can verify cont.resume is called?
d

Dominaezzz

09/17/2019, 11:49 AM
Hmm, I don't think you're testing
suspendCancellableCoroutine
then. You're testing that
onSuccess
is called.
Also to check if
cont.resume
is called, check that the lines of code after
suspendCancellableCoroutine
are executed.
g

gildor

09/17/2019, 11:51 AM
Not sure why do you need argument capturer here, test this function with controlled callback (depends on implementation of addCallback)
r

rkeazor

09/17/2019, 11:54 AM
@Dominaezzz the only issue with that ,if what con.resume is the only thing inside the if state
@gildor than how would you write a test for that block of code
g

gildor

09/17/2019, 11:55 AM
Return null to onSuccess, check that suspend function throws exception
It would be easier to discuss with some self-contained code sample
r

rkeazor

09/17/2019, 11:56 AM
ahh i see what you guys are saying
as in just make sure onSuccess returns the right thing
that makes sense actually
@gildor No need I get what your saying. just do what needs to be done to trigger the callback and see if the suspend function returns the write value
☝🏼 1
i was thinking about invocation testing . but that makes alot of sense. Thanks guys!!
5 Views