Created
August 19, 2020 17:01
-
-
Save AdamMc331/689fed913f4c5195a34cb79b80c93154 to your computer and use it in GitHub Desktop.
Mockk unit fun example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LoginViewModel( | |
| private val authProvider: AuthorizationProvider | |
| ): ViewModel() { | |
| fun handleSuccessfulLogin(authToken: String) { | |
| authProvider.storeAuthorizationToken(authToken) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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