Azure-Samples / active-directory-dotnet-graphapi-console

A .NET console application that performs various queries against the Azure AD Graph API using both user identities and application identities.
79 stars 77 forks source link

How to UnitTest the SDK? #32

Open aramkoukia opened 8 years ago

aramkoukia commented 8 years ago

I've been trying to UnitTest the SDK and using nsubstitute.

Have not found a way to return something i want out of the client.Users object.

The client.Users returns an IUserCollection object which I cannot create a fake object to return in the unit tests.

duncanp-sonar commented 7 years ago

Apologies for the delay in anyone responding to this issue. I guess by now that you've either found a solution or worked round the problem. If not, please ask a question on stackoverflow, tagging the question with one of the aad* or msgraph tags.

I assume you are referring to the Microsoft.Azure.ActiveDirectory.GraphClient library. The client object and user collection are both interfaces so you could provide your own implementations of these interfaces for test purposes. I'm not familiar with NSubstitute but the following code seems to create a dummy client and user collection:

// Create a dummy user collection
var users = Substitute.For<Microsoft.Azure.ActiveDirectory.GraphClient.IUserCollection>();
var userFetcher1 = Substitute.For<Microsoft.Azure.ActiveDirectory.GraphClient.IUserFetcher>();
users.GetByObjectId(null).ReturnsForAnyArgs(userFetcher1);

// Create a dummy client
var client = Substitute.For<Microsoft.Azure.ActiveDirectory.GraphClient.IActiveDirectoryClient>();
client.Users.Returns(users);

// Get an item from the collection
var obj = client.Users.GetByObjectId("123");