Hey guys, I have a question! Given this ```sealed...
# mockk
d
Hey guys, I have a question! Given this
Copy code
sealed interface A {
  
  data class B(...): A
  data class C(...): A
}

fun something(a: A): Int

every { something(any()) } returns 0
every { something(any<B>() } returns 1
I would expect that
something(B(...))
would return 1 correct And that
something(C(...))
would return 0 wrong This is because the
Type
in
any<Type>()
actually doesn’t “work”. The correct way to do this is the more verbose one
Copy code
every { something(any()) } answer {
  when (firstArg<A>()) {
    is B -> 1
    else -> 0 // or error() or whatever
  }
}
Is this expected or is it a bug? Mock 1.13.2 on JVM 11
solved 1
s
Are you looking for
ofType<Type>()
?
Copy code
every { something(ofType<B>()) } returns 1
(I agree, it can be confusing that
any<Type>()
doesn’t actually check the type 😞)
d
ofType
does work! Thank you so much!
k
any<Type>()
is still useful, though. The explicit generic parameter is required for compile-time overload resolution.