microsoftgraph / microsoftgraph/msgraph-sdk-dotnet

v5 How do I unit test my code when using Graph API

Open
#1,746 22 comments 17 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P1 Request: enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.