Kevin Worth
05/16/2024, 12:33 PMFoo
which has sealed Bar
and given two different exhaustive when
statements:
fun Bar.baz() : String {
when (this) {
Bam -> "bam"
Pow -> "pow"
}
}
// Bar
val baz: String
get() = when(this) {
Bam -> "bam"
Pow -> "pow"
}
}
and given the mocked Foo.Bar
is of type Bar_1_Proxy
during the test, why does the extension function result in NoWhenBranchMatchedException
but the getter results in an empty string?Simon Marquis
05/16/2024, 12:34 PMis
keyword instead:
fun Bar.baz() : String {
when (this) {
is Bam -> "bam"
is Pow -> "pow"
}
}
Simon Marquis
05/16/2024, 12:35 PMKevin Worth
05/16/2024, 12:36 PMis
will avoid the exception and both will return empty string? Or both will throw the exception?Simon Marquis
05/16/2024, 12:38 PMwhen
uses reference identity, and mockk can't (or does not support yet) inflating such objects.
For the getter, I'm not sure, but this
might reference the actual object and not the mockk proxy object.Simon Marquis
05/16/2024, 12:39 PMKevin Worth
05/16/2024, 12:43 PMis
?Simon Marquis
05/16/2024, 1:00 PMis Bar
is similar to if (this is Bar)
, it will check the instance has a compatible type
• Bar
is similar to if (this === Bar)
, it will check for referential equality (pointing to the same object)
https://kotlinlang.org/docs/equality.htmlSimon Marquis
05/16/2024, 1:01 PMKevin Worth
05/16/2024, 1:06 PMis
)
What’s confusing is why the getter doesn’t throw the exception and just says, “Eh, I’ll return empty string”. 🤔
And I don’t believe that has anything to do with how the test is set up, but maybe I’m misunderstanding the question.Kevin Worth
05/16/2024, 1:15 PMis
on all the cases, the exception is still thrown. So, maybe I misunderstood the suggestion/explanation.Simon Marquis
05/16/2024, 1:16 PMKevin Worth
05/16/2024, 1:16 PMSimon Marquis
05/16/2024, 1:17 PMKevin Worth
05/16/2024, 1:18 PMSimon Marquis
05/16/2024, 4:19 PMSimon Marquis
05/16/2024, 4:19 PMString?
it would probably have returned null
instead