I was thinking of doing something like this: ```va...
# kotest
k
I was thinking of doing something like this:
Copy code
val result: MyResultType
mockkObject(SomeObject) {
    every { SomeObject.foo() } returns "bar"
    result = callUnitUnderTest()
}

result.a shouldBe 1
result.b shouldBe 2
The idea was to keep the time the object is mocked to a minimum. The object doesn't have to remain mocked while I'm doing the assertions. Unfortunately, the Kotlin compiler gives an error saying
result
is reassigned inside the lambda and uninitialized outside the lambda. This is because it doesn't know that the lambda is guaranteed to be called exactly once. This can be fixed by the use of contracts. Is there any chance it can be done in a future release?
a
Personally I don't see much value in doing that. Because there are so many other, much easier, ways to solve the problem. Such as not invoking any static functions at all, extracting them into injectable dependencies - and those can be easily replaced by other functions. So that the very need to mock has been designed away. Good luck!
k
Thanks. I agree that in general we should inject our dependencies so that we don't need to mock objects. This one was an exception. However, I've just realised that I asked in the wrong channel, because it's about Mockk, not Kotest. Sorry about that.