Hi all, beginner question here. Is it possible tha...
# mockk
a
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
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
HI @mansaldebor thanks for your response. I’m still exploring boundaries of the unit testing 😄 Any idea how to do that?
m
Something like
verify { preferences.somePreference = true }
I'm not completely sure about the syntax regarding setters and mockk.
a
@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!
m
You're welcome! I'm glad it worked 😊