https://kotlinlang.org logo
#mockk
Title
# mockk
j

Jérémy CROS

04/13/2022, 3:42 PM
Hi guys 🙂 Is there anyway to make the second mock declaration work?
Copy code
class DummyUseCase {

    operator fun invoke(): String = TODO()
}

@Test
fun test() {
    val dummyUseCase1: DummyUseCase = mockk()
    every { dummyUseCase1.invoke() } returns ""
    val dummyUseCase2: DummyUseCase = mockk {
        every { this.invoke() } returns ""
    }
}
In the second case, I have a compilation error: “Not enough information to infer type variable T” (I’m fine with the first declaration but we’ve been trying this
operator invoke
thing for our use cases that was showcased in one of the recent MAD Skills video and we use sometimes the second mock declaration) Thanks! 🙂
s

Sam

04/13/2022, 3:50 PM
If this is the same issue I've run into in the past, the workaround is
Copy code
every { this@mockk.invoke() } returns ""
e

ephemient

04/13/2022, 3:54 PM
exactly, wrong
this
if you simply wrote
every { invoke() }
that should work too
j

Jérémy CROS

04/13/2022, 3:55 PM
Yes! Now it works 🙏 🙏
unfortunately, simple invoke does not, it’s the first thing I tried
e

ephemient

04/13/2022, 3:57 PM
hmm that's odd, I didn't think there was another no-arg invoke overload in that scope
j

Jérémy CROS

04/13/2022, 3:58 PM
yeah, that’s what was confusing me:
I'm not really sure what it's supposed to be for 🤔
e

ephemient

04/13/2022, 4:00 PM
if you put a newline after
every
you'll see another receiver there
11 Views