Hi - I am failing in understanding a piece of code...
# getting-started
m
Hi - I am failing in understanding a piece of code - or more concrete: why my modification approach does not work:
Copy code
abstract class DataStateUseCase<in Params, ReturnType> where ReturnType : Any? {
    abstract suspend fun FlowCollector<DataState<ReturnType>>.execute(params: Params)

    suspend operator fun invoke(params: Params) = flow {
        execute(params)
    }.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
}
I want the
execute
method to be visible for testing - and so I tried to annotate it with
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
. Yet in my test code I cannot access the method (same package, same project). Why is that? Is it even possible?
s
The function is public — the
@VisibleForTesting
doesn’t change that, it just documents it. I think your issue is probably not due to visibility, but rather due to the function having multiple receivers. Do you have an example of how you’re trying to call the function?
1
m
when I’m trying to type
sut.login(…)
in my IDE it doesn’t provide this method.
I - as a rookie - assume that it is somewhat with the fact, that it’s a method applied to
FlowCollector
s
To call an extension function, you need the extension receiver (the
FlowCollector
) to be in receiver position (either explicit or implicit). That means that the dispatch receiver (the
DataStateUseCase
) needs to be brought into scope using
with
(or as a context receiver). E.g.
Copy code
flow {
    with(useCase) {
        execute(...)
    }
}
m
OK - thanks… I’ll elaborate on that… as an explanation: I basically do not want to CALL the method. I just want to verify in my code, that it GETS called… (via
coVerify
- from MockK)
s
The same principle applies, roughly — you need a flow collector in receiver position. Something like this should work:
Copy code
coVerify {
    with(useCase) {
        any<FlowCollector<...>>().execute(...)
    }
}
I think 😄
🤞 1
m
well - `execute`is accessible with your snippet… but the test fails… but I guess, I’ll have to dig into mockk for that part
OK - FYI: your snippet is fine and working. My test was failing due to the `execute`method never being called… Well - I had to
collect
things first to trigger the flow
👏 1
🐕 1