-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
How to Unit test or Mock DefaultAzureCredential Method #20911
Comments
Thank you for your feedback. Tagging and routing to the team members best able to assist. |
Hi @PrakashRajanSakthivel - If you need to mock DefaultAzureCredential, I'd recommend something like Moq. For example: var dacMock = new Mock<DefaultAzureCredential>();
int i = 0;
dacMock
.Setup(m => m.GetToken(It.IsAny<TokenRequestContext>(), It.IsAny<CancellationToken>()))
.Callback (() => i++);
var dac = dacMock.Object;
dac.GetToken(new TokenRequestContext());
Console.WriteLine(i.ToString()); |
Hi, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you! |
Thanks, @christothes , Is that something I can inject into my class so that it gets picked up? like in my example above
|
As long as it gets invoked sometime before you attempt to call GetToken, it would be up to you how to call it. In my simple repro, I just called it at the beginning of the function invoke. Since it is static it does not need to be injected. |
Hi, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you! |
I am using DefaultAzureCredential to fetch the access token in my function app which has managed identity from azure to authenticate itself to the APIM. so I am using a HttpMessageHandler in that I am checking If the token doesnt exist make a call to Azure and get the token.
Startup in the Function App
Handler File:
Is there any way I can inject the new AzureDefaultCredentail Method so that I can mock it or pass on like IHttpClientFactory to inject it? I dont find much reference on the unit testin part
The text was updated successfully, but these errors were encountered: