jay
jay

Reputation: 1175

Unit Testing Graph API SDK c#

I am trying to mock this graph API SDK code.

 private readonly GraphServiceClient _client;
 IMailFolderMessagesCollectionPage msgs = await _client.Users[userEmailAddress].MailFolders[folderNameId]
                .Messages.Request().WithMaxRetry(5).WithMaxRetry(new TimeSpan(0, 0, 0, 5, 0)).Filter(emailFilter).GetAsync();

I am not sure how to mock a chained of index properties and method in a single line. I tried to do the following but does not work. Any advices? Thanks.

  private Mock<IGraphServiceClient> mockGsc;
  mockGsc = new Mock<IGraphServiceClient>();
  mockGsc.Setup(x => x.Users[It.IsAny<string>()].MailFolders[It.IsAny<string>].Messages.Request().WithMaxRetry(It.IsAny<int>).WithMaxRetry(It.IsAny<TimeSpan>).Filter(It.IsAny<string>).GetAsync()).Returns< IMailFolderMessagesCollectionPage>();

Upvotes: 1

Views: 1746

Answers (1)

Dev
Dev

Reputation: 2464

In terms of mocking, just have one method that makes all graph calls and use dependency injection to substitute an arbitrary mock. You can save the json responses of some real calls and have your mock return those. It should work fine with your favorite mock / DI framework or just make custom mocks. You can look at the actual dot netsdk for graph for examples: that uses Moq:

Upvotes: 1

Related Questions