首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何模拟FHIR Http客户端对CreateAsync调用中的响应

如何模拟FHIR Http客户端对CreateAsync调用中的响应
EN

Stack Overflow用户
提问于 2021-07-19 15:59:23
回答 1查看 89关注 0票数 1

在Hl7.Fhir.Rest.FhirClient库中实现对CreateAsync的调用时,我正在为如何模拟有效的响应而苦苦挣扎。我知道如何使用模拟HttpMessageHandler对象模拟dotnet-httpclient,并注意到有一个消息处理程序参数可以在创建FhirClient时指定。我尝试做的是为创建步骤指定一个消息处理程序,它是一个模拟消息处理程序对象。

这个简化的单元测试试图模拟HttpMessageHandler,并使其从FhirClient的CreateAsync方法调用中返回有效的主体和结果代码。

代码语言:javascript
复制
        [Fact]
        public async Task SubscribeAndReturnSubscriptionIdAsync()
        {
            var mockHttpMessageHandler = MockFhirHttpClientMessageHandler.MockSubscribeMessageResponse(new StringContent("{'id':'abc123','status':'active'}"), HttpStatusCode.Created);
            var subscriptionResource = new Subscription()
            {
                Criteria = "https://server.fire.ly/CareTeam",
                Status = Subscription.SubscriptionStatus.Active,
                Reason = "test",
                Channel = new Subscription.ChannelComponent()
                {
                    Type = Subscription.SubscriptionChannelType.RestHook,
                    Endpoint = "http://localhost:9999/AscomFhirApi/UpdateCareTeam",
                    Payload = "application/fhir+json"
                },
            };
            var serverUri = new Uri("http://server.fire.ly");
            var clientSettings = new FhirClientSettings()
            {
                PreferredFormat = ResourceFormat.Json
            };

            var fhirHttpClient = new Hl7.Fhir.Rest.FhirClient(serverUri, clientSettings, mockHttpMessageHandler.Object);

            var subscription = await fhirHttpClient.CreateAsync<Subscription>(subscriptionResource);

            Assert.NotEmpty(subscription.Id);
        }

下面显示的MockSubscribeMessageResponse方法创建了在上面的测试中传递给FhirClient的HttpMessageHandler。

代码语言:javascript
复制
public static Mock<HttpMessageHandler> MockSubscribeMessageResponse(
            HttpContent content,
            HttpStatusCode code = HttpStatusCode.OK)
        {
            var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
            mockHttpMessageHandler.Protected()
                .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(),
                    ItExpr.IsAny<CancellationToken>())
                .ReturnsAsync(new HttpResponseMessage
                {
                    StatusCode = code,
                    Content = content
                });
            return mockHttpMessageHandler;
        }

我得到的错误是在看起来像HttpResponseMessage或响应体的内容中出现空引用异常。

代码语言:javascript
复制
System.NullReferenceException
Object reference not set to an instance of an object.
   at Hl7.Fhir.Rest.HttpToEntryExtensions.ToEntryResponse(HttpResponseMessage response, Byte[] body)
   at Hl7.Fhir.Rest.HttpClientRequester.ExecuteAsync(EntryRequest interaction)
   at Hl7.Fhir.Rest.BaseFhirClient.executeAsync[TResource](Bundle tx, IEnumerable`1 expect)
   at Tests.Unit.Core.Services.FirelyHttpClientShould.SubscribeAndReturnSubscriptionIdAsync() in C:\src\AscomIASharedAssignFHIRApi5\Tests.Unit.Core\Services\FirelyHttpClientShould.cs:line 60
EN

回答 1

Stack Overflow用户

发布于 2021-11-09 12:21:01

您可能很久以前就发现了这一点,但是错误的来源很可能是缺少RequestMessageToEntryResponse的实现依赖于response.RequestMessage.RequestUri的设置。所以我猜你需要做的是:

代码语言:javascript
复制
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
    .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
    .ReturnsAsync(new HttpResponseMessage
    {
        StatusCode = code,
        RequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://localhost"),
        Content = content
    });
    return mockHttpMessageHandler;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68437036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档