-
Notifications
You must be signed in to change notification settings - Fork 549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit Testing with AWS Amplify - Mocking #3641
Comments
@DigiValetMobiles Thanks for submitting the issue. Can you provide additional details on what specific sign in function you're testing and how you've implemented that sign in function? |
Here's a code snippet of my sign-in function:
I am trying to create a mock object of AWSCognitoAuthSession to test the success scenario of my signIn function using dummy values. However, I'm encountering issues when attempting to mock it like this:
It would be helpful if you could provide @VisibleForTesting annotations on any private or internal classes, as this could simplify the mocking process. Thank you! |
@DigiValetMobiles making Looking at your sign-in function, there isn't any business logic involved so I would reconsider what the value of unit testing this function and reconsider it as a integration test instead. Are you trying to unit test the |
Thanks for your feedback! I want to test the toDigiAuthResponse() function along with the emission logic, as it contains all of my business logic that depends on the AWSCognitoAuthSession data. Overall, I’m aiming to write unit tests for the signIn function since it includes crucial business logic. I appreciate your insights on the testing strategy! |
@DigiValetMobiles could you expand on the exact issue you're seeing? The constructor does not need to be accessible to mock with mockk. As an example, given the following fake production code: class TestCode {
suspend fun getSession(): DigiAuthResponse {
val session = Amplify.Auth.fetchAuthSession() as AWSCognitoAuthSession
return session.toDigiAuthResponse()
}
}
data class DigiAuthResponse(
val isSignedIn: Boolean,
)
fun AWSCognitoAuthSession.toDigiAuthResponse() = DigiAuthResponse(
isSignedIn = this.isSignedIn
) I can write a unit test for the @Test
fun `test that DigiAuthResponse.isSignedIn is true`() = runTest {
val mockSession = mockk<AWSCognitoAuthSession>{
every { isSignedIn } returns true
}
mockkObject(Amplify) {
coEvery { Amplify.Auth.fetchAuthSession() } returns mockSession
val result = TestCode().getSession()
assertTrue(result.isSignedIn)
}
} Which works just fine even though I cannot instantiate an If you can elaborate on where this is going wrong we may be able to point you in the right direction 😄 |
I'm currently facing challenges in unit testing my authentication functions that utilize the AWS Amplify library, specifically regarding the AWSCognitoAuthSession class. The issue arises because this class has an internal constructor property, making it inaccessible in my test classes.
Service:
aws-auth-cognito
Environment:
SDK Version: 2.20.0
Problem:
I need to test my sign-in function, which uses the Amplify Auth sign-in function and fetches the user session. However, I cannot mock the AWSCognitoAuthSession effectively due to its constructor's visibility constraints.
Questions:
What strategies can I use to mock AWSCognitoAuthSession in my unit tests?
Are there any recommended practices for testing functions that rely on AWS Amplify's Auth module?
Would it be possible to consider making constructors more accessible for testing purposes in future updates?
Testing framework:
JUnit, Mockk
Programming Language:
Kotlin
If there are any specific examples or resources that could help clarify how to effectively test AWS Amplify Auth functionality, I would greatly appreciate it!
The text was updated successfully, but these errors were encountered: