Title
j

Joram Visser

02/16/2019, 7:09 PM
Is it possible to mock a suspend inline extension function? If so, how?
o

oleksiyp

02/16/2019, 7:32 PM
Believe inline is not supported in any way
j

Joram Visser

02/16/2019, 9:45 PM
Does that mean I can not unit test a function like:
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

LeoColman

02/16/2019, 11:42 PM
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

Joram Visser

02/17/2019, 3:53 PM
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. 👍