Skip to content
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

Mock testing CognitoUser instance #151

Closed
WajahatAliAbid opened this issue Mar 14, 2020 · 4 comments
Closed

Mock testing CognitoUser instance #151

WajahatAliAbid opened this issue Mar 14, 2020 · 4 comments
Labels

Comments

@WajahatAliAbid
Copy link

Is there a way to mock test CognitoUser instance using moq, from my understanding the method StartWithAdminNoSrpAuthAsync isn't virtual so it cannot be mocked. How can we mock this functionality?

var cognitoUser = new Mock<CognitoUser>(It.IsAny<string>(), It.IsAny<string>(), userPool.Object, Mock.Of<IAmazonCognitoIdentityProvider>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>());
cognitoUser.Setup(x=> x.StartWithAdminNoSrpAuthAsync(Mock.Of<InitiateAdminNoSrpAuthRequest>())).ReturnsAsync(It.IsAny<AuthFlowResponse>());

Error screenshot
image

@klaytaybai klaytaybai added needs-triage This issue or PR still needs to be triaged. feature-request A feature should be added or improved. guidance Question that needs advice or information. and removed needs-triage This issue or PR still needs to be triaged. labels Mar 16, 2020
@klaytaybai
Copy link
Contributor

Hi @WajahatAliAbid, thanks for the feedback. I agree that it would be nice if this was supported more simply. You should be able to mock it if you write a subclass to wrap around the CognitoUser class. You can then just return the invocations of the base methods and mock on the new class and its methods.

        public virtual new Task<AuthFlowResponse> StartWithAdminNoSrpAuthAsync(InitiateAdminNoSrpAuthRequest adminAuthRequest)
        {
            return base.StartWithAdminNoSrpAuthAsync(adminAuthRequest);
        }

@klaytaybai klaytaybai added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Mar 17, 2020
@WajahatAliAbid
Copy link
Author

Hi @klaytaybai
I tested it with using a custom subclass of CognitoUser

public class CustomCognitoUser : CognitoUser
    {
        public CustomCognitoUser(string userID, string clientID, CognitoUserPool pool, IAmazonCognitoIdentityProvider provider, string? clientSecret = null, string? status = null, string? username = null, Dictionary<string, string>? attributes = null) : base(userID, clientID, pool, provider, clientSecret, status, username, attributes)
        {
        }
        public virtual new Task<AuthFlowResponse> StartWithAdminNoSrpAuthAsync(InitiateAdminNoSrpAuthRequest adminAuthRequest)
        {
            return base.StartWithAdminNoSrpAuthAsync(adminAuthRequest);
        }
    }

This issue has been resolved. Thanks for your feedback. I'm looking forward to future releases of aws-aspnet-cognito-identity-provider with support for mocking without writing a wrapper class for it.

@klaytaybai klaytaybai removed response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. guidance Question that needs advice or information. labels Mar 17, 2020
@WajahatAliAbid
Copy link
Author

@klaytaybai can you look at this PR please?

@github-actions
Copy link

github-actions bot commented Feb 5, 2022

We have noticed this issue has not received attention in 1 year. We will close this issue for now. If you think this is in error, please feel free to comment and reopen the issue.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Feb 5, 2022
@github-actions github-actions bot closed this as completed Feb 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants