Hi all, beginner question here. Is it possible that production code method changes mock?
I’ve got following:
Copy code
// PRODUCTION
class classUnderTest(
private val preferences: Preferences
) {
fun changePreferenceToTrue() {
preferences.somePreference = true
}
}
// TEST
@Test
fun `Given preference false when changePreferenceToTrue then check is preference updated`() {
every { preferences.somePreference } returns false
classUnderTest.changePreferenceToTrue()
// I want to assert that this Preference is true
print(preferences.somePreference) // always returns as defined above, in this case false
}
m
mansaldebor
01/23/2020, 9:27 PM
Hey!
Since you told the mock to always return false, that's what it'll do, so it's pointless to assert its value.
What I would do is verify that you call the setter with value true 😊
a
antoniomarin
01/24/2020, 9:13 AM
HI @mansaldebor thanks for your response. I’m still exploring boundaries of the unit testing 😄 Any idea how to do that?
m
mansaldebor
01/24/2020, 6:18 PM
Something like
verify { preferences.somePreference = true }
I'm not completely sure about the syntax regarding setters and mockk.
a
antoniomarin
01/27/2020, 9:50 AM
@mansaldebor just tried it and works! Thanks a lot! I've seen verify before but haven't understood what it does until now! Thanks once again!