microsoftgraph / microsoftgraph/msgraph-sdk-dotnet
v5 How do I unit test my code when using Graph API
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 789
- Forks
- 264
- Avg merge
- 15h 17m
- Merged PRs (30d)
- 3
Description
V5 of the Azure Graph API has removed the IGraphServiceClient and has methods that are not virtual. This makes mocking calls to the Graph API client extremely difficult as you have to mock the underlying Request Handler code (https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/1667).
Is there a recommended way to Mock calls to the GraphServiceClient in order for me to check my logic calling several Graph API methods.
For example, I have code like:
var userResponse = await _graphClient.Users.GetAsync(requestConfiguration =>
{
requestConfiguration.QueryParameters.Select = new[] { "Id", "AccountEnabled" };
requestConfiguration.QueryParameters.Filter = $"UserPrincipalName eq '{loginName}'";
requestConfiguration.QueryParameters.Top = 1;
});
var azureUser = userResponse.Value.First();
if (azureUser.AccountEnabled == false)
{
azureUser.AccountEnabled = true;
await _graphClient.Users[azureUser.Id].PatchAsync(azureUser);
}
How would I go about mocking the GetAsync and PatchAsync methods?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading this issue and the related msgraph-sdk-dotnet issue #1667, then inspect how GraphServiceClient exposes GetAsync and PatchAsync in the shown example. Document a recommended way to mock these calls so the logic involving user lookup and account enabling can be tested.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100