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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay