when i'm mocking methods on a class, i can write i...
# mockk
p
when i'm mocking methods on a class, i can write it like this:
Copy code
val mockMyClass = mockk<MyClass> {
    every { myMethod(any()) } returns "success"
}
inside the curly braces the mock is implicitly the receiver of
myMethod
, so i can call it in my code with
mockMyClass.myMethod("something")
. i'm trying to do something similar where i'm mocking a function, rather than a class. so:
Copy code
typealias MyFunction = (String) -> String

val mockMyFunction = mockk<MyFunction> {
    every {
        // what goes in here???
        // this(any()) - this doesn't work
        // this.invoke(any()) - this also doesn't work???
    } returns "success"
}
is there any way to do this? i know i can achieve the same thing like this:
Copy code
val mockMyFunction = mockk<MyFunction>()
every { mockMyFunction(any()) } returns "success"
but the other way would be nicer in some situations, e.g. inside a test class without having to put the
every { ... } returns "success"
in a
@BeforeAll
function