如何为Graph Mail API的EML流下载调用设置正确Mock鉴别器值?
解决方案
针对你的Mock需求,需要根据使用的Microsoft Graph .NET SDK版本选择正确的解析器:
适用于SDK v5+(最新版)
在新版SDK中,邮件内容的下载会通过BinaryData处理二进制流,因此你需要将{???}替换为BinaryData,同时将测试流转换为BinaryData实例返回:
var mockRequestAdapter = RequestAdapterMockFactory.Create(); mockRequestAdapter.Setup(adapter => adapter.SendAsync( // Download eml stream It.Is<RequestInformation>(info => info.HttpMethod == Method.GET), BinaryData.CreateFromDiscriminatorValue, It.IsAny<Dictionary<string, ParsableFactory<IParsable>>>(), It.IsAny<CancellationToken>())) .ReturnsAsync(BinaryData.FromStream(EmlTestMailStream()));
说明:
Content.GetAsync()内部会自动将BinaryData转换为Stream返回,因此Mock时返回BinaryData即可满足业务代码的需求。
适用于SDK v4及以下(旧版本)
如果使用的是旧版SDK,对应的二进制流封装类型是StreamResponse,此时需要替换为StreamResponse:
var mockRequestAdapter = RequestAdapterMockFactory.Create(); mockRequestAdapter.Setup(adapter => adapter.SendAsync( It.Is<RequestInformation>(info => info.HttpMethod == Method.GET), StreamResponse.CreateFromDiscriminatorValue, It.IsAny<Dictionary<string, ParsableFactory<IParsable>>>(), It.IsAny<CancellationToken>())) .ReturnsAsync(new StreamResponse { Content = EmlTestMailStream() });
内容的提问来源于stack exchange,提问作者user3079834
相关产品推荐
相关产品推荐

