Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Created August 19, 2020 17:01
Show Gist options
  • Select an option

  • Save AdamMc331/689fed913f4c5195a34cb79b80c93154 to your computer and use it in GitHub Desktop.

Select an option

Save AdamMc331/689fed913f4c5195a34cb79b80c93154 to your computer and use it in GitHub Desktop.
Mockk unit fun example
interface AuthorizationProvider {
fun getAuthorizationToken(): String?
fun storeAuthorizationToken(token: String)
}
class LoginViewModel(
private val authProvider: AuthorizationProvider
): ViewModel() {
fun handleSuccessfulLogin(authToken: String) {
authProvider.storeAuthorizationToken(authToken)
}
}
class LoginViewModelTest {
private val mockAuthProvider: AuthorizationProvider = mockk()
@Test
fun testValidLogin() {
val viewModel = LoginViewModel(mockAuthProvider)
viewModel.handleSuccessfulLogin("Test Token")
verify(exactly = 1) {
mockAuthProvider.storeAuthorizationToken("Test Token")
}
}
}
// Using the above code, this fails because storeAuthorizationToken is not mocked
// I can resolve using `mockk(relaxUnitFun = true)`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment