How to mock Microsoft Graph APIs GraphServiceClient using moq

Newbie Dev 151 Reputation points
2021-12-14T20:12:54.143+00:00

Hi,

My project is using GraphServiceClient to get the user Group Names using the below code.

var group = await graphClient.Me.TransitiveMemberOf
                            .Request()
                            .GetAsync();

The group variable is then used to get the DisplayName of the group.

I want to unit test the above code using Moq.

var mockAuthProvider = new Mock<IAuthenticationProvider>();
            var mockHttpProvider = new Mock<IHttpProvider>();
           var mockGraphClient = new Mock<GraphServiceClient>(mockAuthProvider.Object, mockHttpProvider.Object);

mockGraphClient.Setup(c => c.Me.TransitiveMemberOf.Request().GetAsync(CancellationToken.None)).ReturnsAsync(???);

The ReturnAsync will return IUserTransitiveMemberOfCollectionWithReferencesPage, but how can I give a default value for it so I can test the rest of the method which actually gets the displayName

Thanks in advance.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,794 questions
0 comments No comments
{count} votes