David Kubecka
12/20/2023, 11:01 AMdata class Animal(val type: String) {
val isFavorite = type == "cat"
}
How do you set up the class in a test which just relies on its behavior, i.e. it's not the test for the class? Three approaches come to my mind:
• Just construct the class explicitly and let the isFavorite
property be actually computed.
• Mock the class including all its properties (constructor and computed)
• Use spyk
and mock just the computed property (didn't try but I guess it should work)Mattia Tommasone
12/20/2023, 11:02 AMDavid Kubecka
12/20/2023, 11:04 AMmocking is about driving object behaviorsAgree, but here it's IMO not that clear because
isFavorite
can be considered behaviour because it's computed (albeit simple)Mattia Tommasone
12/20/2023, 11:05 AMMattia Tommasone
12/20/2023, 11:05 AMisFavorite
in your case is kind of an edge case between “data” and “behavior” 🙂David Kubecka
12/20/2023, 11:07 AMMattia Tommasone
12/20/2023, 11:09 AMMattia Tommasone
12/20/2023, 11:10 AMDavid Kubecka
12/20/2023, 11:12 AMJacob
12/20/2023, 4:36 PM