Paul Martin
07/25/2020, 12:06 PMval 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:
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:
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