【问题标题】:How to mock response in FHIR Http Client calls to CreateAsync如何在 FHIR Http 客户端调用中模拟对 CreateAsync 的响应
【发布时间】:2021-09-26 21:54:51
【问题描述】:

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

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

        [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 方法创建了 HttpMessageHandler,它在上述测试中传递给FhirClient

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 或响应正文。

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

【问题讨论】:

    标签: hl7-fhir


    【解决方案1】:

    你可能很久以前就知道了,但是错误的来源很可能缺少RequestMessageToEntryResponse 的实现取决于response.RequestMessage.RequestUri 的设置。所以我想你需要做的是:

    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;
    

    【讨论】:

      猜你喜欢
      • 2011-06-13
      • 2013-05-11
      • 2016-08-06
      • 1970-01-01
      • 2010-11-07
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多