prakashrajansakthivel
prakashrajansakthivel

Reputation: 2042

Mocking Fails for Azure Function (IHttpClientFactory)

I am facing issues while mocking the IHttpClientFactory Interface in Azure Function. Here is What am doing, I have trigger, once the message the received I am calling an API to update data. I am using SendAsync Method for that. While writing unit test case, I am not able to mock the client.For testing , I have tried making the get call in the constructor itself, still didn't work. Function Class

 public class UpdateDB
{
    private readonly IHttpClientFactory _clientFactory;
    private readonly HttpClient _client;

    public UpdateDB(IHttpClientFactory clientFactory)
    {
        _clientFactory = clientFactory;
        _client = clientFactory.CreateClient();
        _client.GetAsync("");
    }

    [FunctionName("DB Update")]
    public async Task Run([ServiceBusTrigger("topic", "dbupdate", Connection = "connection")]string mySbMsg, ILogger log)
    {
        var client = _clientFactory.CreateClient();
        log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
        DBConvert payload = JsonConvert.DeserializeObject<DBConvert>(mySbMsg);
        string jsonContent = JsonConvert.SerializeObject(payload);
        var httpContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");
        HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, "api/DBU/data");
        message.Content = httpContent;
        var response = await client.SendAsync(message);
    }
}

TestClass

namespace XUnitTestProject1
{
    public class DelegatingHandlerStub : DelegatingHandler
    {
        private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _handlerFunc;
        public DelegatingHandlerStub()
        {
            _handlerFunc = (request, cancellationToken) => Task.FromResult(request.CreateResponse(HttpStatusCode.OK));
        }

        public DelegatingHandlerStub(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handlerFunc)
        {
            _handlerFunc = handlerFunc;
        }

        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            return _handlerFunc(request, cancellationToken);
        }
    }

    public class test
    {
        [Fact]
        public async Task Should_Return_Ok()
        {
            //
            Mock<ILogger> _logger = new Mock<ILogger>();
            var expected = "Hello World";
            var mockFactory = new Mock<IHttpClientFactory>();
            var configuration = new HttpConfiguration();
            var clientHandlerStub = new DelegatingHandlerStub((request, cancellationToken) =>
            {
                request.SetConfiguration(configuration);
                var response = request.CreateResponse(HttpStatusCode.Accepted);
                return Task.FromResult(response);
            });
            var client = new HttpClient(clientHandlerStub);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(client);

            var clientTest = mockFactory.Object.CreateClient();

            //Works Here, but not in the instance.
            clientTest.GetAsync("");

            IHttpClientFactory factory = mockFactory.Object;

            var service = new UpdateDB(factory);

            await service.Run("", _logger.Object);

        }
    }
}

I have followed the sample here. How to mock the new HttpClientFactory in .NET Core 2.1 using Moq

Upvotes: 1

Views: 856

Answers (1)

Jota.Toledo
Jota.Toledo

Reputation: 28464

For mocking/intercepting HttpClient usage, I would recommend you to use mockhttp Your test would then be:

class Test {
  private readonly Mock<IHttpClientFactory> httpClientFactory;
  private readonly MockHttpMessageHandler handler;

  constructor(){
    this.handler = new MockHttpMessageHandler();
    this.httpClientFactory = new Mock<IHttpClientFactory>();
    this.httpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>()))
        .Returns(handler.ToHttpClient());
  }

  [Fact]
  public async Task Test(){
    // Arrange
    this.handler.Expect("api/DBU/data")
                .Respond(HttpStatusCode.Ok);
    var sut = this.CreateSut();

    // Act
    await sut.Run(...);

    // Assert
    this.handler.VerifyNoOutstandingExpectation();       
  }

  private UpdateDB CreateSut() => new UpdateDB(this.httpClientFactory.Object);
}

You can further configure how the HTTP request expectation should behave, but for that you should read a bit the documentation of mockhttp

Upvotes: 2

Related Questions