DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

3 2

C# : How to unit test Graph SDK with Select Query

I post Unit testing Microsoft Graph SDK Client with moq library the other day, but I encounter a challenge when I need to write a bit more complex query.

Graph SDK with SELECT query

I use following code to get GraphUser object, which I specify several fields explicitly by using SELECT Linq.

Microsoft.Graph.User graphUser = await this.graphServiceClient.Users["<userId>"]
    .Request()
    .Select(u => new
    {
        u.Id,
        u.BusinessPhones,
        u.GivenName
    })
    .GetAsync();
Enter fullscreen mode Exit fullscreen mode

Unit Test

This is the mock I use to cover above code. There maybe better way to do it but it's working anyway :)

Mock<IAuthenticationProvider> mockedIAuthenticationProvider = new ();
Mock<IHttpProvider> mockedIHttpProvider = new ();
Mock<GraphServiceClient> mockedGraphServiceClient = new (mockedIAuthenticationProvider.Object, mockedIHttpProvider.Object);
mockedGraphServiceClient.Setup(x => x.Users[It.IsAny<string>()]
    .Request()
    .Select(It.IsAny<Expression<Func<Microsoft.Graph.User, object>>>())
    .GetAsync(It.IsAny<CancellationToken>()))
    .ReturnsAsync(new Microsoft.Graph.User() { Id = Guid.NewGuid().ToString(), BusinessPhones = new List<string>(), GivenName = "Test" });
Enter fullscreen mode Exit fullscreen mode

I use It.IsAny<Expression<Func<Microsoft.Graph.User, object>>>() as an argument to Select method which covers any combination of properties.

Hope this article will help myself in the future as I shall forget this sooner or later.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (2)

Collapse
 
sanoojvs profile image
Sanooj V S • Edited

@kenakamu do you have unit test code for NUnit framework? I have exactly same logic to get GraphUser object.

I can Substitute for (IGraphServiceUsersCollectionRequestBuilder). But I am not able to Returns(the Microsoft User object) to collection request builder object.

Collapse
 
kenakamu profile image
Kenichiro Nakamura

I haven't personally tried nunit but it should work quite similar way

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay