how do you guys test suspendCancellableCorourtine.
# coroutines
r
how do you guys test suspendCancellableCorourtine.
d
Test?
r
as in . is it possible to test what is captured by the continuation
g
Why do you need this? just run suspend function and test result
r
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
Call
getUser
add trigger the callback for both paths in your tests then.
r
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
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
Not sure why do you need argument capturer here, test this function with controlled callback (depends on implementation of addCallback)
r
@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
Return null to onSuccess, check that suspend function throws exception
It would be easier to discuss with some self-contained code sample
r
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!!