Is it possible to mock a suspend inline extension ...
# mockk
j
Is it possible to mock a suspend inline extension function? If so, how?
o
Believe inline is not supported in any way
j
Does that mean I can not unit test a function like:
Copy code
suspend inline fun <reified T : Any> MongoCollection<T>.getOneById(id: ObjectId): Either<MongoError, T> {
    val filter = idFilterQuery(id)
    return try {
        val resultEntity = this.find(filter).awaitFirstOrNull()
        when {
            resultEntity != null -> right(resultEntity)
            else -> left(ENTITY_NOT_FOUND)
        }
    } catch (e: Exception) {
        left(ERROR)
    }
}
Does it mean that I can not call the above function from another class/function and reply with a mocked version. Does it also mean I won’t be able to call this function in a test case and mock the calls that are made within this inline function?
l
Inline functions are replaced in compilation time. It can't be mocked because technically it doesn't exist when you're executing code, because it was inline
You'll have to mock the internals of the function, such as mocking what it calls
j
I guess inlining a function has its trade offs. I did manage to mock the internal calls of this inline function. Thanks for the help. 👍
192 Views