Search code examples
.netemailmockingmicrosoft-graph-api

Mock graph mail API Stream response


I can successfully download a mail in eml format given the mails ID like this:

var mailStream = await client.Users.Me.Messages[mailId].Content.GetAsync();
using var outputFileStream = new FileStream(filepath, FileMode.Create);
mailStream.CopyTo(outputFileStream);

I would like to mock this call for unit testing. However I have troubles finding the correct DiscriminatorValue for this mock, see {???}. The return value should be Stream?.

var mockRequestAdapter = RequestAdapterMockFactory.Create();
mockRequestAdapter.Setup(adapter => adapter.SendAsync( // Download eml stream
    It.Is<RequestInformation>(info => info.HttpMethod == Method.GET),
    {???}.CreateFromDiscriminatorValue,
    It.IsAny<Dictionary<string, ParsableFactory<IParsable>>>(),
    It.IsAny<CancellationToken>()))
    .ReturnsAsync(EmlTestMailStream());

What is the correct method to call here for the mock to work?


Solution

  • Use SendPrimitiveAsync instead of SendAsync when the return value is either Stream or int.

    var mockRequestAdapter = RequestAdapterMockFactory.Create();
    mockRequestAdapter.Setup(adapter => adapter.SendPrimitiveAsync<Stream>( // Download eml stream
        It.Is<RequestInformation>(info => info.HttpMethod == Method.GET),
        It.IsAny<Dictionary<string, ParsableFactory<IParsable>>>(),
        It.IsAny<CancellationToken>()))
        .ReturnsAsync(EmlTestMailStream());