msgraph-sdk-dotnet icon indicating copy to clipboard operation
msgraph-sdk-dotnet copied to clipboard

What is the best way to mock Batch request?

Open kenakamu opened this issue 3 years ago • 3 comments

Batch cannot be mocked as easy as other methods. What is the recommended way to mock Batch request so that I can write unit test?

kenakamu avatar Dec 04 '21 07:12 kenakamu

Hey @kenakamu,

For starters you could always checkout the tests here.

You could also use checkout the tests for BatchResponseContent and BatchRequestContent here

andrueastman avatar Dec 15 '21 05:12 andrueastman

Thanks. I am having following code in my project and want to create uint test around it.

graphServiceClient.Batch.Request().PostAsync(batchRequestContent)

I can easily mock normal ones, but not Batch. I do mock like below for other request.

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 = userId.ToString(), BusinessPhones = new List<string>() });

So ideally, if I can simply mock Batch request same as above, that would be ideal. At the moment, I cannot setup Batch itself as it's not virtual method.

kenakamu avatar Dec 16 '21 03:12 kenakamu

By the way, this is what I am doing at the moment. https://dev.to/kenakamu/c-how-to-unit-test-graph-sdk-batch-request-38j2

kenakamu avatar Dec 21 '21 06:12 kenakamu